From 94c2a7cb438cb0c9f9590c521d28c2d1796d2edd Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Sun, 6 Dec 2020 16:47:31 +0800 Subject: [PATCH] =?UTF-8?q?P7113=20=E6=8E=92=E6=B0=B4=E7=B3=BB=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit R43304253 --- problem/P7113/P7113.cpp | 74 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 problem/P7113/P7113.cpp diff --git a/problem/P7113/P7113.cpp b/problem/P7113/P7113.cpp new file mode 100644 index 00000000..3d063694 --- /dev/null +++ b/problem/P7113/P7113.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +int n, m, d, t; +vector u[100005]; +pair<__int128, __int128> ans[100005]; + +ostream& operator<<(ostream& __ostream, __int128 __n) { + string __o; + while (__n) { + __o.push_back(__n % 10 + '0'); + __n /= 10; + } + reverse(__o.begin(), __o.end()); + return __ostream << __o; +} + +__int128 gcd(__int128 a, __int128 b) { + if (b == 0) return a; + return gcd(b, a % b); +} + +void dfs(int x, __int128 first, __int128 second) { + if (u[x].empty()) { + if (ans[x].first && ans[x].second) { + if (ans[x].second == second) { + ans[x].first += first; + __int128 tmp = gcd(ans[x].first, ans[x].second); + ans[x].first /= tmp; + ans[x].second /= tmp; + } + else { + first *= ans[x].second; + ans[x].first *= second; + ans[x].first += first; + ans[x].second *= second; + __int128 tmp = gcd(ans[x].first, ans[x].second); + ans[x].first /= tmp; + ans[x].second /= tmp; + } + } + else { + ans[x].first = first; + ans[x].second = second; + } + return; + } + int s = u[x].size(); + second *= s; + for (int i = 0; i < s; i++) { + dfs(u[x][i], first, second); + } +} + +int main() { + cin >> n >> m; + for (int i = 1; i <= n; i++) { + cin >> d; + for (int j = 1; j <= d; j++) { + cin >> t; + u[i].push_back(t); + } + } + for (int i = 1; i <= m; i++) { + dfs(i, 1, 1); + } + for (int i = 1; i <= n; i++) { + if (u[i].empty()) { + cout << ans[i].first << ' ' << ans[i].second << endl; + } + } + return 0; +}