This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |