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
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';}
#ifdef DEBUG
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x)
#else
#define debug(...) {}
#endif

/*
 * Opis: Reprezentacja dużych int'ów
 * Czas: Podstawa wynosi 1e9. Mnożenie, dzielenie, nwd oraz modulo jest kwadratowe, wersje operatorX(Num, int) liniowe
 * Użycie: Podstawę można zmieniać (ma zachodzić base == 10^digits_per_elem).
 */

struct Num {
	static constexpr int digits_per_elem = 9, base = int(1e9);
	vector<int> x;

	Num& shorten() {
		while(ssize(x) and x.back() == 0)
			x.pop_back();
		for(int a : x)
			assert(0 <= a and a < base);
		return *this;
	}

	Num(const string& s) {
		for(int i = ssize(s); i > 0; i -= digits_per_elem)
			if(i < digits_per_elem)
				x.emplace_back(stoi(s.substr(0, i)));
			else
				x.emplace_back(stoi(s.substr(i - digits_per_elem, digits_per_elem)));
		shorten();
	}
	Num() {}
	Num(LL s) : Num(to_string(s)) {
		assert(s >= 0);
	}
};

string to_string(const Num& n) {
	stringstream s;
	s << (ssize(n.x) ? n.x.back() : 0);
	for(int i = ssize(n.x) - 2; i >= 0; --i)
		s << setfill('0') << setw(n.digits_per_elem) << n.x[i];
	return s.str();
}

ostream& operator<<(ostream &o, const Num& n) {
	return o << to_string(n).c_str();
}

Num operator+(Num a, const Num& b) {
	int carry = 0;
	for(int i = 0; i < max(ssize(a.x), ssize(b.x)) or carry; ++i) {
		if(i == ssize(a.x))
			a.x.emplace_back(0);
		a.x[i] += carry + (i < ssize(b.x) ? b.x[i] : 0);
		carry = bool(a.x[i] >= a.base);
		if(carry)
			a.x[i] -= a.base;
	}
	return a.shorten();
}

bool operator<(const Num& a, const Num& b) {
	if(ssize(a.x) != ssize(b.x))
		return ssize(a.x) < ssize(b.x);
	for(int i = ssize(a.x) - 1; i >= 0; --i)
		if(a.x[i] != b.x[i])
			return a.x[i] < b.x[i];
	return false;
}

bool operator==(const Num& a, const Num& b) {
	return a.x == b.x;
}

bool operator<=(const Num& a, const Num& b) {
	return a < b or a == b;
}

Num operator-(Num a, const Num& b) {
	assert(b <= a);
	int carry = 0;
	for(int i = 0; i < ssize(b.x) or carry; ++i) {
		a.x[i] -= carry + (i < ssize(b.x) ? b.x[i] : 0);
		carry = a.x[i] < 0;
		if(carry)
			a.x[i] += a.base;
	}
	return a.shorten();
}

Num operator*(Num a, int b) {
	assert(0 <= b and b < a.base);
	int carry = 0;
	for(int i = 0; i < ssize(a.x) or carry; ++i) {
		if(i == ssize(a.x))
			a.x.emplace_back(0);
		LL cur = a.x[i] * LL(b) + carry;
		a.x[i] = int(cur % a.base);
		carry = int(cur / a.base);
	}
	return a.shorten();
}

Num operator*(const Num& a, const Num& b) {
	Num c;
	c.x.resize(ssize(a.x) + ssize(b.x));
	REP(i, ssize(a.x))
		for(int j = 0, carry = 0; j < ssize(b.x) or carry; ++j) {
			LL cur = c.x[i + j] + a.x[i] * LL(j < ssize(b.x) ? b.x[j] : 0) + carry;
			c.x[i + j] = int(cur % a.base);
			carry = int(cur / a.base);
		}
	return c.shorten();
}

Num operator/(Num a, int b) {
	assert(0 < b and b < a.base);
	int carry = 0;
	for(int i = ssize(a.x) - 1; i >= 0; --i) {
		LL cur = a.x[i] + carry * LL(a.base);
		a.x[i] = int(cur / b);
		carry = int(cur % b);
	}
	return a.shorten();
}

// zwraca a * pow(a.base, b)
Num shift(Num a, int b) {
	vector v(b, 0);
	a.x.insert(a.x.begin(), v.begin(), v.end());
	return a.shorten();
}

Num operator/(Num a, const Num& b) {
	assert(ssize(b.x));
	Num c;
	for(int i = ssize(a.x) - ssize(b.x); i >= 0; --i) {
		if (a < shift(b, i)) continue;
		int l = 0, r = a.base - 1;
		while (l < r) {
			int m = (l + r + 1) / 2;
			if (shift(b * m, i) <= a)
				l = m;
			else
				r = m - 1;
		}
		c = c + shift(l, i);
		a = a - shift(b * l, i);
	}
	return c.shorten();
}

template<typename T>
Num operator%(const Num& a, const T& b) {
	return a - ((a / b) * b);
}

Num nwd(const Num& a, const Num& b) {
	if(b == Num())
		return a;
	return nwd(b, a % b);
}

struct frac {
	Num nom, den;
	frac() {
		nom = 0;
		den = 1;
	}
	frac(Num a, Num b) : nom(a), den(b) {}
	friend frac operator + (frac p, frac q) {
		Num a = p.nom * q.den + q.nom * p.den;
		Num b = p.den * q.den;
		Num g = nwd(a, b);
		return frac(a / g, b / g);
	}
	friend frac operator / (frac p, LL q) {
		p.den = p.den * q;
		Num g = nwd(p.nom, p.den);
		return frac(p.nom / g, p.den / g);
	}
};

int main() {
	cin.tie(0)->sync_with_stdio(0);
	int n;
	cin >> n;
	vector graph(n, vector<int>());
	Num mul = 1;
	REP (i, n) {
		int m;
		cin >> m;
		if (m)
			mul = mul * m;
		REP (j, m) {
			int x;
			cin >> x;
			--x;
			graph[i].emplace_back(x);
		}
	}
	debug(mul);
	vector<Num> dp(n);
	dp[0] = mul;
	REP (i, n)
		for (int j : graph[i])
			dp[j] = dp[j] + (dp[i] / ssize(graph[i]));
	debug(dp);

	Num odp = mul;
	REP (i, n)
		odp = nwd(odp, dp[i]);
	cout << mul / odp << endl;
}