样例1对了,样例2对了一半,交上去一分都没有。
样例2:
输入
10 1
5 2 3 4 5 6
2 7 8
2 8 10
2 9 7
1 9
3 7 8 9
1 10
0
1 10
0
输出
4 15
11 15
我的输出
4 15
43 30
#include <bits/stdc++.h>
#define _for(i, a, b) for (int i = (a); i <= (b); i ++ )
using namespace std;
const int N = 1e5 + 5;
typedef long long ll;
int n, m, vis[N];
vector<int> G[N];
struct node { ll son, mot; } water[N];
inline ll gcd(ll x, ll y) { return (y == 0 ? x : gcd(y, x % y)); }
inline ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
inline void add(ll & son, ll & mot, ll add_son, ll add_mot) {
ll Gcd = gcd(add_son, add_mot);
add_son /= Gcd, add_mot /= Gcd;
ll new_mot = lcm(mot, add_mot), new_son = son * (new_mot / mot) + add_son * (new_mot / add_mot);
ll GCd = gcd(new_son, new_mot);
son = new_son / GCd, mot = new_mot / GCd;
}
void dfs(int x) { for (int y : G[x]) add(water[y].son, water[y].mot, water[x].son, water[x].mot * G[x].size()), dfs(y); }
inline void bfs() {
queue<int> q;
_for (i, 1, m) q.push(i), vis[i] = 1;
while (! q.empty()) {
int x = q.front(); q.pop();
for (int y : G[x]) if (! vis[y]) vis[y] = 1, add(water[y].son, water[y].mot, water[x].son, water[x].mot * G[x].size()), q.push(y);
}
}
int main() {
ios :: sync_with_stdio(false), cin.tie(0);
cin >> n >> m;
_for (i, 1, n) water[i].son = 0, water[i].mot = 1;
_for (i, 1, n) {
int d, t;
cin >> d;
while (d -- ) cin >> t, G[i].push_back(t);
}
_for (i, 1, m) water[i].son = 1;
_for (i, 1, m) dfs(i);
// bfs();
_for (i, m + 1, n) if (! G[i].size()) cout << water[i].son << " " << water[i].mot << endl;
return 0;
}