이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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][19];
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=18;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(int i=1;i<=18;i++){
if(twok[x][i-1] == -1)continue;
twok[x][i]=twok[twok[x][i-1]][i-1];
}
for(auto v:adj[x])if(v!=p){
dfs(v,x);
}
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... |