#include <bits/stdc++.h>
#define ull unsigned long long
using namespace std;
long long
const int MAX=105;
ull wynik=1;
vector <vector <int>> graf(MAX);
short int tab[MAX];
queue <int> q;
ull mian[MAX];
ull licz[MAX];
ull NWW(ull a, ull b)
{
return (a*b)/__gcd(a, b);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, r, x;
cin >>n;
for(int i=1; i<=n; i++)
{
mian[i]=1;
cin >>r;
for(int j=0; j<r; j++)
{
cin >>x;
tab[x]++;
graf[i].push_back(x);
}
}
licz[1]=1;
q.push(1);
if(!graf[1].size())
{
cout <<1;
return 0;
}
while(!q.empty())
{
x=q.front();
q.pop();
wynik=NWW(wynik*licz[x], mian[x]*graf[x].size())/licz[x];
for(int v: graf[x])
{
if(graf[v].size())
{
ull temp=NWW(mian[v], graf[x].size()*mian[x]);
licz[v]*=temp/mian[v];
licz[v]+=licz[x]*temp/(graf[x].size()*mian[x]);
mian[v]=temp;
temp=__gcd(mian[v], licz[v]);
mian[v]/=temp;
licz[v]/=temp;
tab[v]--;
if(!tab[v]) q.push(v);
}
}
}
cout <<wynik;
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | #include <bits/stdc++.h> #define ull unsigned long long using namespace std; long long const int MAX=105; ull wynik=1; vector <vector <int>> graf(MAX); short int tab[MAX]; queue <int> q; ull mian[MAX]; ull licz[MAX]; ull NWW(ull a, ull b) { return (a*b)/__gcd(a, b); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, r, x; cin >>n; for(int i=1; i<=n; i++) { mian[i]=1; cin >>r; for(int j=0; j<r; j++) { cin >>x; tab[x]++; graf[i].push_back(x); } } licz[1]=1; q.push(1); if(!graf[1].size()) { cout <<1; return 0; } while(!q.empty()) { x=q.front(); q.pop(); wynik=NWW(wynik*licz[x], mian[x]*graf[x].size())/licz[x]; for(int v: graf[x]) { if(graf[v].size()) { ull temp=NWW(mian[v], graf[x].size()*mian[x]); licz[v]*=temp/mian[v]; licz[v]+=licz[x]*temp/(graf[x].size()*mian[x]); mian[v]=temp; temp=__gcd(mian[v], licz[v]); mian[v]/=temp; licz[v]/=temp; tab[v]--; if(!tab[v]) q.push(v); } } } cout <<wynik; return 0; } |
English