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 all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define pil pair<int,long long>
const int maxn=1e5+9;
int n,m,q;
vector<int>a[maxn];
int st[maxn][21],bit[maxn],val[maxn],last[maxn],dep[maxn],in[maxn],out[maxn],tme=0;
pii edg[maxn];
bool active[maxn];
void dfs(int u,int par){
val[u]=1;
in[u]=++tme;
for(auto v:a[u]){
if (v==par)continue;
st[v][0]=u;
dep[v]=dep[u]+1;
for1(j,1,20){
st[v][j]=st[st[v][j-1]][j-1];
}
dfs(v,u);
}
out[u]=tme;
}
void add(int pos,int v){
for(;pos<=n;pos+=(pos-(pos&(pos-1))))bit[pos]+=v;
}
int get(int pos){
int sum=0;
for(;pos>=1;pos-=(pos-(pos&(pos-1))))sum+=bit[pos];
return sum;
}
int root(int u){
for2(i,20,0){
if (!st[u][i])continue;
if (get(in[u])-get(in[st[u][i]])==dep[u]-dep[st[u][i]]){
u=st[u][i];
}
}
return u;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
cin>>n>>m>>q;
for1(i,1,n-1){
int u,v;
cin>>u>>v;
a[u].pb(v);
a[v].pb(u);
edg[i]={u,v};
}
dfs(1,0);
for1(i,1,m){
int x;
cin>>x;
int u=edg[x].fi,v=edg[x].se;
if (st[u][0]==v)swap(u,v);
if (active[x]){
last[v]=val[v]=val[root(u)];
add(in[v],-1);
add(out[v]+1,1);
}
else {
val[root(u)]+=val[v]-last[v];
add(in[v],1);
add(out[v]+1,-1);
}
active[x]^=1;
}
for1(i,1,q){
int k;
cin>>k;
cout<<val[root(k)]<<'\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... |