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
#ifdef LOCAL
void debug_out() {cerr<<endl;}
template <typename Head, typename... Tail>
void debug_out(Head _H, Tail... _T) {cerr<<" "<<to_string(_H);debug_out(_T...);}
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
const int MAXN = 200005;
const int inf=1000000500ll;
const long long oo =1000000000000000500ll;
const int MOD = (int)1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair<int,int> pi;
/*
you need a "dynamic UFDS" on tree which supports connection and deletion between parent and child nodes
when disconnecting, carry the ans from the current ufds group's root to this ufds group's root.
when connecting, add the ans for the child's ufds group to the parent's ufds group. (keep track of how many more nodes there are now)
*/
int twok[MAXN][18];
int st[MAXN], en[MAXN];
int TIME=1;
vector<int> adj[MAXN];
int n,m,q;
int ans[MAXN], lst[MAXN], state[MAXN];
namespace fw {
int fw[MAXN];
void update(int x, int nval){
for(;x<MAXN;x+=x&-x)fw[x]+=nval;
}
void update(int x,int y, int nval){
update(x,nval);
update(y+1,-nval);
}
int query(int x){
int res=0;
for(;x;x-=x&-x)res+=fw[x];
return res;
}
};
int find(int x){ // finds the parent
for(int i=17;i>=0;i--){
if(twok[x][i]==-1)continue;
if(fw::query(st[x]) == fw::query(st[twok[x][i]])){
x=twok[x][i];
}
}
return x;
}
void dfs(int x, int p){
st[x]=TIME++;
twok[x][0]=p;
for(auto v:adj[x])if(v!=p){
dfs(v,x);
}
for(int i=1;i<=17;i++){
if(twok[x][i-1] == -1)continue;
twok[x][i]=twok[twok[x][i-1]][i-1];
}
en[x]=TIME-1;
}
pi E[MAXN];
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
memset(twok,-1,sizeof twok);
cin >> n >> m >> q;
for(int i=1;i<=n-1;i++){
int a,b; cin >> a >> b;
E[i].first=a;
E[i].second=b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for(int i=1;i<=n;i++)ans[i]=1;
dfs(1,-1);
for(int i=1;i<=n;i++){
fw::update(st[i],en[i],1);//there is blockage between child and parent.
}
for(int t=1;t<=m;t++){
int edge; cin >> edge;
int par=E[edge].first;
int child=E[edge].second;
if(st[par]>st[child])swap(par,child);
if(!state[edge]){ // activate
int rt=find(par);
ans[rt] += ans[child] - lst[child];
fw::update(st[child],en[child],-1);
} else{
int rt=find(par);
ans[child]=lst[child]=ans[rt];
fw::update(st[child],en[child],1);
}
state[edge] = !state[edge];
}
while(q--){
int x; cin >> x;
cout<<ans[find(x)]<<"\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... |