#include <bits/stdc++.h>
#define FOR(i, x, n) for(int i = x; i < n; i++)
#define F0R(i, n) FOR(i, 0, n)
#define ROF(i, x, n) for(int i = n - 1; i >= x; i--)
#define R0F(i, n) ROF(i, 0, n)
#define WTF cout << "WTF" << endl
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define F first
#define S second
#define PB push_back
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
const int N = 5e5 + 7;
const int LOG = 22;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
LL fact[N], ifact[N];
LL chs[N], ans, val[N], vir[N];
LL ns[N], n, q, bad, par[N], sz[N];
bool active[N];
VI adj[N];
#define endl '\n'
inline int pw(LL a, int b) {
LL ret = 1;
for(; b; b >>= 1, a = a * a % MOD)
if (b & 1) ret = ret * a % MOD;
return ret;
}
void precalc() {
fact[0] = 1;
FOR(i, 1, N) fact[i] = fact[i - 1] * i % MOD;
ifact[N - 1] = pw(fact[N - 1], MOD - 2);
R0F(i, N - 1) ifact[i] = ifact[i + 1] * (i + 1) % MOD;
return;
}
inline int nCr(int nn, int rr) {
if (nn < rr || rr < 0) return 0;
return (fact[nn] * ifact[rr] % MOD) * ifact[nn - rr] % MOD;
}
void init() {
cin >> n >> val[0];
F0R(i, n - 1) {
int u, v;
cin >> u >> v;
adj[--u].PB(--v);
adj[v].PB(u);
}
cin >> q;
return;
}
void dfs(int now, int p) {
par[now] = p;
sz[now] = 1;
for(int on : adj[now]) if (on != p) {
dfs(on, now);
sz[now] += sz[on];
}
return;
}
void updChoose(int now) {
if (!chs[now]) bad--;
else ans = (ans * pw(chs[now], MOD - 2)) % MOD;
chs[now] = nCr(val[now] + sz[now] - 1, sz[now] - 1);
if (!chs[now]) bad++;
else ans = (ans * chs[now]) % MOD;
return;
}
void remChoose(int now) {
if (!chs[now]) bad--;
else ans = (ans * pw(chs[now], MOD - 2)) % MOD;
chs[now] = 1;
return;
}
void addTag() {
int v, x;
cin >> v >> x;
v--;
val[v] += x;
int now = par[v];
while(!active[now]) {
sz[now] -= sz[v];
val[now] -= val[v];
now = par[now];
}
sz[now] -= sz[v];
val[now] -= val[v];
vir[v] = x;
updChoose(now);
updChoose(v);
active[v] = 1;
return;
}
void remTag() {
int v;
cin >> v; v--;
int now = par[v];
while(!active[now]) {
sz[now] += sz[v];
val[now] += val[v];
now = par[now];
}
sz[now] += sz[v];
val[now] += val[v];
val[v] -= vir[v];
vir[v] = 0;
updChoose(now);
remChoose(v);
active[v] = 0;
return;
}
int main() {
IOS;
init();
precalc();
dfs(0, -1);
active[0] = 1;
fill(chs, chs + n, 1);
chs[0] = nCr(val[0] + n - 1, n - 1);
ans = chs[0];
cout << ans << endl;
while(q--) {
int tp; cin >> tp;
if (tp == 1) addTag();
else remTag();
cout << (bad ? 0 : ans) << endl;
/*cout << "after: " << endl;
cout << "val: "; F0R(i, n) cout << val[i] << ' '; cout << endl;
cout << "sz: "; F0R(i, n) cout << sz[i] << ' '; cout << endl;*/
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
38716 KB |
Output is correct |
2 |
Correct |
119 ms |
38648 KB |
Output is correct |
3 |
Correct |
123 ms |
38720 KB |
Output is correct |
4 |
Correct |
127 ms |
38756 KB |
Output is correct |
5 |
Correct |
117 ms |
34728 KB |
Output is correct |
6 |
Correct |
17 ms |
20368 KB |
Output is correct |
7 |
Correct |
16 ms |
20052 KB |
Output is correct |
8 |
Correct |
16 ms |
20052 KB |
Output is correct |
9 |
Correct |
110 ms |
31028 KB |
Output is correct |
10 |
Correct |
115 ms |
31092 KB |
Output is correct |
11 |
Correct |
124 ms |
31068 KB |
Output is correct |
12 |
Correct |
111 ms |
30540 KB |
Output is correct |
13 |
Correct |
100 ms |
37628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
19844 KB |
Output is correct |
2 |
Correct |
15 ms |
19924 KB |
Output is correct |
3 |
Correct |
15 ms |
19924 KB |
Output is correct |
4 |
Correct |
15 ms |
19876 KB |
Output is correct |
5 |
Correct |
16 ms |
19952 KB |
Output is correct |
6 |
Correct |
17 ms |
20008 KB |
Output is correct |
7 |
Correct |
17 ms |
20148 KB |
Output is correct |
8 |
Correct |
17 ms |
20020 KB |
Output is correct |
9 |
Correct |
17 ms |
20248 KB |
Output is correct |
10 |
Correct |
18 ms |
20308 KB |
Output is correct |
11 |
Correct |
18 ms |
20308 KB |
Output is correct |
12 |
Correct |
16 ms |
20180 KB |
Output is correct |
13 |
Correct |
19 ms |
20172 KB |
Output is correct |
14 |
Correct |
18 ms |
20180 KB |
Output is correct |
15 |
Correct |
19 ms |
20480 KB |
Output is correct |
16 |
Correct |
18 ms |
20192 KB |
Output is correct |
17 |
Correct |
21 ms |
20252 KB |
Output is correct |
18 |
Correct |
17 ms |
20096 KB |
Output is correct |
19 |
Correct |
18 ms |
20216 KB |
Output is correct |
20 |
Correct |
17 ms |
20052 KB |
Output is correct |
21 |
Correct |
16 ms |
20092 KB |
Output is correct |
22 |
Correct |
16 ms |
19884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
41376 KB |
Output is correct |
2 |
Correct |
136 ms |
41708 KB |
Output is correct |
3 |
Correct |
138 ms |
42284 KB |
Output is correct |
4 |
Correct |
150 ms |
42480 KB |
Output is correct |
5 |
Correct |
194 ms |
39240 KB |
Output is correct |
6 |
Correct |
17 ms |
20324 KB |
Output is correct |
7 |
Correct |
16 ms |
20080 KB |
Output is correct |
8 |
Correct |
16 ms |
20076 KB |
Output is correct |
9 |
Correct |
195 ms |
35464 KB |
Output is correct |
10 |
Correct |
176 ms |
35172 KB |
Output is correct |
11 |
Correct |
166 ms |
35060 KB |
Output is correct |
12 |
Correct |
171 ms |
34616 KB |
Output is correct |
13 |
Execution timed out |
3074 ms |
48316 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
340 ms |
36340 KB |
Output is correct |
2 |
Correct |
332 ms |
40816 KB |
Output is correct |
3 |
Correct |
294 ms |
40820 KB |
Output is correct |
4 |
Correct |
346 ms |
40840 KB |
Output is correct |
5 |
Correct |
317 ms |
39876 KB |
Output is correct |
6 |
Correct |
299 ms |
40708 KB |
Output is correct |
7 |
Correct |
201 ms |
32156 KB |
Output is correct |
8 |
Correct |
257 ms |
32184 KB |
Output is correct |
9 |
Correct |
335 ms |
40868 KB |
Output is correct |
10 |
Correct |
309 ms |
40980 KB |
Output is correct |
11 |
Correct |
331 ms |
41140 KB |
Output is correct |
12 |
Correct |
229 ms |
32200 KB |
Output is correct |
13 |
Correct |
190 ms |
29412 KB |
Output is correct |
14 |
Correct |
234 ms |
30624 KB |
Output is correct |
15 |
Correct |
208 ms |
30860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
38716 KB |
Output is correct |
2 |
Correct |
119 ms |
38648 KB |
Output is correct |
3 |
Correct |
123 ms |
38720 KB |
Output is correct |
4 |
Correct |
127 ms |
38756 KB |
Output is correct |
5 |
Correct |
117 ms |
34728 KB |
Output is correct |
6 |
Correct |
17 ms |
20368 KB |
Output is correct |
7 |
Correct |
16 ms |
20052 KB |
Output is correct |
8 |
Correct |
16 ms |
20052 KB |
Output is correct |
9 |
Correct |
110 ms |
31028 KB |
Output is correct |
10 |
Correct |
115 ms |
31092 KB |
Output is correct |
11 |
Correct |
124 ms |
31068 KB |
Output is correct |
12 |
Correct |
111 ms |
30540 KB |
Output is correct |
13 |
Correct |
100 ms |
37628 KB |
Output is correct |
14 |
Correct |
16 ms |
19844 KB |
Output is correct |
15 |
Correct |
15 ms |
19924 KB |
Output is correct |
16 |
Correct |
15 ms |
19924 KB |
Output is correct |
17 |
Correct |
15 ms |
19876 KB |
Output is correct |
18 |
Correct |
16 ms |
19952 KB |
Output is correct |
19 |
Correct |
17 ms |
20008 KB |
Output is correct |
20 |
Correct |
17 ms |
20148 KB |
Output is correct |
21 |
Correct |
17 ms |
20020 KB |
Output is correct |
22 |
Correct |
17 ms |
20248 KB |
Output is correct |
23 |
Correct |
18 ms |
20308 KB |
Output is correct |
24 |
Correct |
18 ms |
20308 KB |
Output is correct |
25 |
Correct |
16 ms |
20180 KB |
Output is correct |
26 |
Correct |
19 ms |
20172 KB |
Output is correct |
27 |
Correct |
18 ms |
20180 KB |
Output is correct |
28 |
Correct |
19 ms |
20480 KB |
Output is correct |
29 |
Correct |
18 ms |
20192 KB |
Output is correct |
30 |
Correct |
21 ms |
20252 KB |
Output is correct |
31 |
Correct |
17 ms |
20096 KB |
Output is correct |
32 |
Correct |
18 ms |
20216 KB |
Output is correct |
33 |
Correct |
17 ms |
20052 KB |
Output is correct |
34 |
Correct |
16 ms |
20092 KB |
Output is correct |
35 |
Correct |
16 ms |
19884 KB |
Output is correct |
36 |
Correct |
134 ms |
41376 KB |
Output is correct |
37 |
Correct |
136 ms |
41708 KB |
Output is correct |
38 |
Correct |
138 ms |
42284 KB |
Output is correct |
39 |
Correct |
150 ms |
42480 KB |
Output is correct |
40 |
Correct |
194 ms |
39240 KB |
Output is correct |
41 |
Correct |
17 ms |
20324 KB |
Output is correct |
42 |
Correct |
16 ms |
20080 KB |
Output is correct |
43 |
Correct |
16 ms |
20076 KB |
Output is correct |
44 |
Correct |
195 ms |
35464 KB |
Output is correct |
45 |
Correct |
176 ms |
35172 KB |
Output is correct |
46 |
Correct |
166 ms |
35060 KB |
Output is correct |
47 |
Correct |
171 ms |
34616 KB |
Output is correct |
48 |
Execution timed out |
3074 ms |
48316 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |