This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// In the name of God
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
const int N = 1e6 + 5, MOD = 1e9 + 7;
int n, r;
int fact[N], inv[N], f[N], c[N];
int val[N], par[N];
vector<int> adj[N];
int ans, bad;
int binpow(int a, int b) {
return b ? 1LL * binpow(1LL * a * a % MOD, b / 2) * (b % 2 ? a : 1) % MOD : 1;
}
int C(int a, int b) {
return a >= 0 && a <= b ? 1LL * fact[b] * inv[a] % MOD * inv[b - a] % MOD : 0;
}
void dfs(int v, int p) {
c[v] = 1;
par[v] = p;
for (auto u : adj[v]) {
if (u != p) {
dfs(u, v);
c[v] += c[u];
}
}
}
int F(int v) {
return C(c[v], val[v] - f[v] + c[v]);
}
void add(int v) {
if (F(v) == 0)
bad++;
else
ans = 1LL * ans * F(v) % MOD;
}
void rem(int v) {
if (F(v) == 0)
bad--;
else
ans = 1LL * ans * binpow(F(v), MOD - 2) % MOD;
}
void solve() {
cin >> n >> r;
for (int i = 0; i < n - 1; i++) {
int v, u; cin >> v >> u;
adj[v].pb(u), adj[u].pb(v);
}
ans = C(n - 1, r + n - 1);
val[1] = r;
dfs(1, 0);
c[1]--;
cout << ans << '\n';
int q; cin >> q;
memset(val, -1, sizeof val);
val[1] = r;
while (q--) {
int t, v; cin >> t >> v;
if (t == 1) {
int x; cin >> x;
int u = par[v];
while (val[u] == -1) {
c[u] -= c[v];
f[u] += x - f[v];
u = par[u];
}
rem(u);
c[u] -= c[v];
f[u] += x - f[v];
c[v]--;
val[v] = x;
add(u);
add(v);
}
else {
int u = par[v];
while (val[u] == -1) {
c[u] += c[v] + 1;
f[u] += f[v] - val[v];
u = par[u];
}
rem(v);
rem(u);
//cout << " ! " << F(v) << ' ' << F(u) << endl;
f[u] += f[v] - val[v];
c[u] += c[v] + 1;
c[v]++;
val[v] = -1;
add(u);
}
if (bad == 0)
cout << ans << '\n';
else
cout << 0 << '\n';
}
}
int32_t main() {
fact[0] = 1;
for (int i = 1; i < N; i++)
fact[i] = 1LL * i * fact[i - 1] % MOD;
inv[N - 1] = binpow(fact[N - 1], MOD - 2);
for (int i = N - 2; i >= 0; i--)
inv[i] = 1LL * inv[i + 1] * (i + 1) % MOD;
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
solve();
return 0;
}
# | 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... |