#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll N = 2e5 + 2;
ll etseg[N], huu[N], out[N], par[18][N], is_connected[N], in[N],val[N], depth[N], last_connected[N];
ll Sum[8 * N], Has[8 * N], n;
vector < ll > adj[N];
ll timer = 0;
void Go(ll cur, ll parent) {
ll i;
par[0][cur] = parent;
for (i = 1; i <= 18; i++) par[i][cur] = par[i - 1][par[i - 1][cur]];
is_connected[cur] = 0;
in[cur] = ++timer;
// par[cur] = parent;
for ( ll child : adj[cur]) {
if ( child == parent) continue;
depth[child] = depth[cur] + 1;
Go(child, cur);
}
out[cur] = timer;
return ;
}
void PushDown(ll p, ll lo, ll hi) {
ll mid = (lo +hi)/2;
Has[2 * p] += (Has[p]);
Has[2 * p + 1] += (Has[p]);
Sum[2 * p] += (Has[p] * (mid - lo + 1));
Sum[2 * p + 1] += (Has[p] * (hi - mid));
Has[p] = 0;
}
void Update(ll p, ll lo, ll hi, ll l, ll r, ll val) {
if ( lo > r || l > hi) return ;
if ( l <= lo && hi <= r) {
Has[p] += val;
Sum[p] += (val * (hi - lo + 1));
return ;
}
PushDown(p, lo, hi) ;
ll mid = (lo + hi)/2;
Update(2 * p, lo, mid, l, r, val);
Update(2 * p + 1, mid + 1, hi, l, r, val);
Sum[p] = Sum[2 * p] + Sum[2 * p + 1];
}
ll Find(ll p, ll lo, ll hi, ll x) {
if (lo ==hi) return Sum[p];
PushDown(p, lo, hi);
ll mid = (lo + hi)/2;
if ( x <=mid) return Find(2 * p, lo, mid, x);
return Find(2 * p + 1, mid + 1, hi, x);
}
ll Check(ll x) {
ll i, y, dist = 0, X, Y;
for ( i = 18; i >= 0; i --) {
y = par[i][x];
X = in[x];
Y = in[y];
X = Find(1, 1, n, X);
Y = Find(1, 1, n, Y);
if ( depth[x] - depth[y] == X - Y) x = y;
}
return x;
}
int main() {
ll m, r, x, y, s, i, j, ans, t, q;
cin >> n >> m >> q;
for (i = 1; i<= n; i ++) val[i] = 1;
for (i = 1; i < n; i++) {
cin >> etseg[i] >> huu[i];
adj[etseg[i]].push_back(huu[i]);
}
Go(1, 1);
for (i = 1; i < n; i ++) {
if ( depth[huu[i]] - 1 != depth[etseg[i]]) swap(etseg[i], huu[i]);
}
for (j = 1; j <= m; j ++) {
cin >> i;
if ( is_connected[huu[i]]) {
s = Check(huu[i]);
val[huu[i]] = val[s];
Update(1, 1, n, in[huu[i]], out[huu[i]], -1);
}
else {
s = Check(etseg[i]);
r = val[s] + val[huu[i]] + last_connected[huu[i]];
val[s] = r;
last_connected[huu[i]] = r;
Update(1, 1, n, in[huu[i]], out[huu[i]], 1);
}
is_connected[huu[i]] ^= 1;
// for (i = 1; i <= n; i ++) {
// cout << val[i] << " ";
// }
// cout << endl;
}
for (i = 1; i <= q; i ++) {
cin >> x;
s = Check(x);
cout << val[s] << endl;
}
}
컴파일 시 표준 에러 (stderr) 메시지
synchronization.cpp: In function 'void Go(ll, ll)':
synchronization.cpp:15:47: warning: iteration 17 invokes undefined behavior [-Waggressive-loop-optimizations]
15 | for (i = 1; i <= 18; i++) par[i][cur] = par[i - 1][par[i - 1][cur]];
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
synchronization.cpp:15:23: note: within this loop
15 | for (i = 1; i <= 18; i++) par[i][cur] = par[i - 1][par[i - 1][cur]];
| ~~^~~~~
# | 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... |