#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MXN = 1e6 + 5;
const int mod = 1e9 + 7;
int n, r;
vector<int> adj[MXN];
int f[MXN], tag[MXN], sum[MXN], x[MXN], delsz[MXN], sz[MXN], del[MXN];
int pw(int a, int b, int c)
{
a %= c;
int res = 1;
while (b)
{
if (b & 1) res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
int nck(int n, int k)
{
if (min(n, k) < 0 || k > n) return 0;
return (f[n] * pw((f[n - k] * f[k]) % mod, mod - 2, mod)) % mod;
}
void dfs(int a, int p)
{
sz[a] = 1, delsz[a] = 0;
sum[a] = 0;
for (int &v : adj[a])
{
if (v == p) continue;
dfs(v, a);
sum[a] += sum[v];
sz[a] += sz[v];
delsz[a] += delsz[v];
}
x[a] = sum[a];
if (tag[a] != -1)
{
sum[a] = tag[a];
del[a] = delsz[a];
delsz[a] = sz[a];
}
}
int calc()
{
int res = 1;
for (int i = 1; i <= n; i++)
{
if (tag[i] == -1) continue;
if (x[i] > tag[i]) return 0;
res = (res * nck(sz[i] - del[i] - 1 + tag[i] - x[i], tag[i] - x[i])) % mod;
}
return res;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
f[0] = 1;
for (int i = 1; i < MXN; i++)
{
f[i] = (f[i - 1] * i) % mod;
tag[i] = -1;
}
cin >> n >> r;
tag[1] = r;
for (int i = 1; i < n; i++)
{
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
int q;
cin >> q;
dfs(1, 1);
cout << calc() << '\n';
while (q--)
{
int t;
cin >> t;
if (t == 1)
{
int u, v;
cin >> u >> v;
tag[u] = v;
}
else
{
int u;
cin >> u;
tag[u] = -1;
}
dfs(1, 1);
cout << calc() << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
70992 KB |
Output is correct |
2 |
Correct |
72 ms |
70884 KB |
Output is correct |
3 |
Correct |
77 ms |
70992 KB |
Output is correct |
4 |
Correct |
78 ms |
70992 KB |
Output is correct |
5 |
Correct |
65 ms |
66124 KB |
Output is correct |
6 |
Correct |
12 ms |
50012 KB |
Output is correct |
7 |
Correct |
12 ms |
49756 KB |
Output is correct |
8 |
Correct |
12 ms |
49756 KB |
Output is correct |
9 |
Correct |
67 ms |
63360 KB |
Output is correct |
10 |
Correct |
72 ms |
63316 KB |
Output is correct |
11 |
Correct |
68 ms |
63312 KB |
Output is correct |
12 |
Correct |
62 ms |
63060 KB |
Output is correct |
13 |
Correct |
70 ms |
70148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
49756 KB |
Output is correct |
2 |
Correct |
11 ms |
49756 KB |
Output is correct |
3 |
Correct |
11 ms |
49756 KB |
Output is correct |
4 |
Correct |
12 ms |
49616 KB |
Output is correct |
5 |
Correct |
29 ms |
49876 KB |
Output is correct |
6 |
Correct |
214 ms |
49756 KB |
Output is correct |
7 |
Correct |
170 ms |
49756 KB |
Output is correct |
8 |
Correct |
168 ms |
49944 KB |
Output is correct |
9 |
Correct |
296 ms |
50032 KB |
Output is correct |
10 |
Correct |
318 ms |
50008 KB |
Output is correct |
11 |
Correct |
303 ms |
50008 KB |
Output is correct |
12 |
Correct |
22 ms |
50008 KB |
Output is correct |
13 |
Correct |
303 ms |
50008 KB |
Output is correct |
14 |
Correct |
303 ms |
50008 KB |
Output is correct |
15 |
Correct |
322 ms |
50480 KB |
Output is correct |
16 |
Correct |
296 ms |
50012 KB |
Output is correct |
17 |
Correct |
297 ms |
50012 KB |
Output is correct |
18 |
Correct |
220 ms |
49984 KB |
Output is correct |
19 |
Correct |
288 ms |
50028 KB |
Output is correct |
20 |
Correct |
46 ms |
49752 KB |
Output is correct |
21 |
Correct |
47 ms |
49756 KB |
Output is correct |
22 |
Correct |
13 ms |
49756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3065 ms |
72276 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3026 ms |
65536 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
70992 KB |
Output is correct |
2 |
Correct |
72 ms |
70884 KB |
Output is correct |
3 |
Correct |
77 ms |
70992 KB |
Output is correct |
4 |
Correct |
78 ms |
70992 KB |
Output is correct |
5 |
Correct |
65 ms |
66124 KB |
Output is correct |
6 |
Correct |
12 ms |
50012 KB |
Output is correct |
7 |
Correct |
12 ms |
49756 KB |
Output is correct |
8 |
Correct |
12 ms |
49756 KB |
Output is correct |
9 |
Correct |
67 ms |
63360 KB |
Output is correct |
10 |
Correct |
72 ms |
63316 KB |
Output is correct |
11 |
Correct |
68 ms |
63312 KB |
Output is correct |
12 |
Correct |
62 ms |
63060 KB |
Output is correct |
13 |
Correct |
70 ms |
70148 KB |
Output is correct |
14 |
Correct |
11 ms |
49756 KB |
Output is correct |
15 |
Correct |
11 ms |
49756 KB |
Output is correct |
16 |
Correct |
11 ms |
49756 KB |
Output is correct |
17 |
Correct |
12 ms |
49616 KB |
Output is correct |
18 |
Correct |
29 ms |
49876 KB |
Output is correct |
19 |
Correct |
214 ms |
49756 KB |
Output is correct |
20 |
Correct |
170 ms |
49756 KB |
Output is correct |
21 |
Correct |
168 ms |
49944 KB |
Output is correct |
22 |
Correct |
296 ms |
50032 KB |
Output is correct |
23 |
Correct |
318 ms |
50008 KB |
Output is correct |
24 |
Correct |
303 ms |
50008 KB |
Output is correct |
25 |
Correct |
22 ms |
50008 KB |
Output is correct |
26 |
Correct |
303 ms |
50008 KB |
Output is correct |
27 |
Correct |
303 ms |
50008 KB |
Output is correct |
28 |
Correct |
322 ms |
50480 KB |
Output is correct |
29 |
Correct |
296 ms |
50012 KB |
Output is correct |
30 |
Correct |
297 ms |
50012 KB |
Output is correct |
31 |
Correct |
220 ms |
49984 KB |
Output is correct |
32 |
Correct |
288 ms |
50028 KB |
Output is correct |
33 |
Correct |
46 ms |
49752 KB |
Output is correct |
34 |
Correct |
47 ms |
49756 KB |
Output is correct |
35 |
Correct |
13 ms |
49756 KB |
Output is correct |
36 |
Execution timed out |
3065 ms |
72276 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |