#pragma GCC optimize("O3")
#include<bits/stdc++.h>
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
typedef long long ll;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<61);
const int inf=(1<<30);
const int nmax=2e5+50;
const int mod=1e9+7;
using namespace std;
int x,y,i,n,up[nmax][20],tin[nmax],tout[nmax],t,v,ans[nmax],q,m,fw[nmax],lst[nmax];
vector<int>a[nmax];
vector<pair<int,int> >e;
void dfs(int x,int p)
{
tin[x]=++t;
if(p)up[x][0]=p;
else up[x][0]=1;
for(int i=1;i<20;i++)up[x][i]=up[up[x][i-1]][i-1];
for(int i=0;i<a[x].size();i++)if(a[x][i]!=p)dfs(a[x][i],x);
tout[x]=t+1;
}
void upd(int i,int v)
{
for(;i<tout[1];i+=i&(-i))fw[i]+=v;
}
int qry(int i)
{
int rs=0;
for(;i>=1;i-=i&(-i))rs+=fw[i];
return rs;
}
int get(int x)
{
int y=qry(tin[x]);
for(int i=19;i>=0;i--)if(qry(up[x][i])==y)x=up[x][i];
return x;
}
int main()
{
//freopen("sol.in","r",stdin);
//freopen("sol.out","w",stdout);
ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
cin>>n>>m>>q;
for(i=1;i<n;i++)
{
cin>>x>>y;
e.pb({x,y});
a[x].pb(y);
a[y].pb(x);
}
dfs(1,1);
for(i=0;i<n-1;i++)if(tin[e[i].fr]>tin[e[i].sc])swap(e[i].fr,e[i].sc);
for(i=1;i<=n;i++)
{
upd(tin[i],1);
upd(tout[i],-1);
ans[i]=1;
}
while(m--)
{
cin>>i;
x=e[i-1].fr,y=e[i-1].sc,v=get(x);
if(lst[i]==-1)
{
upd(tin[y],1);
upd(tout[y],-1);
ans[y]=lst[i]=ans[v];
}
else
{
upd(tin[y],-1);
upd(tout[y],1);
ans[v]+=ans[y]-lst[i];
lst[i]=-1;
}
}
while(q--)
{
cin>>x;
cout<<ans[get(x)]<<endl;
}
return 0;
}
Compilation message
synchronization.cpp: In function 'void dfs(int, int)':
synchronization.cpp:26:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<a[x].size();i++)if(a[x][i]!=p)dfs(a[x][i],x);
~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Incorrect |
6 ms |
5112 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
20748 KB |
Output is correct |
2 |
Correct |
93 ms |
21324 KB |
Output is correct |
3 |
Correct |
86 ms |
23148 KB |
Output is correct |
4 |
Correct |
85 ms |
23144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
5112 KB |
Output is correct |
2 |
Correct |
5 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5116 KB |
Output is correct |
4 |
Correct |
6 ms |
5116 KB |
Output is correct |
5 |
Correct |
5 ms |
5112 KB |
Output is correct |
6 |
Correct |
9 ms |
5240 KB |
Output is correct |
7 |
Correct |
35 ms |
7032 KB |
Output is correct |
8 |
Correct |
339 ms |
23400 KB |
Output is correct |
9 |
Correct |
368 ms |
23272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
346 ms |
22484 KB |
Output is correct |
2 |
Correct |
252 ms |
23528 KB |
Output is correct |
3 |
Correct |
254 ms |
23604 KB |
Output is correct |
4 |
Correct |
254 ms |
23804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5112 KB |
Output is correct |
2 |
Incorrect |
5 ms |
5112 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |