This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int mxn=1e5+5;
vector<int> adj[mxn];
vector<int> dp[mxn];
int up[mxn][20];
int st[mxn],en[mxn];
bool ok[mxn];
int depth[mxn];
int bit[mxn];
int timer=0;
void update(int pos,int val){
for(;pos<mxn;pos+=(pos&-pos)){
bit[pos]+=val;
}
}
int query(int pos){
int ans=0;
for(;pos>0;pos-=(pos&-pos)){
ans+=bit[pos];
}
return ans;
}
void dfs(int v,int p=0){
up[v][0]=p;
st[v]=++timer;
depth[v]=depth[p]+1;
dp[v].push_back(v);
for(auto u:adj[v]){
if(u==p) continue;
dfs(u,v);
}
en[v]=timer;
}
int go(int v,int len){
for(int i=0;i<20;i++){
if(len&(1<<i)) v=up[v][i];
}
return v;
}
int main() {_
int n,m,q;
cin>>n>>m>>q;
vector<pair<int,int>> e(n-1);
for(int i=0;i<n-1;i++){
int a,b;
cin>>a>>b;
e[i]={a,b};
adj[a].push_back(b);
adj[b].push_back(a);
}
vector<int> qs;
for(int i=0;i<m;i++){
int x;
cin>>x;
x--;
qs.push_back(x);
}
int root;
cin>>root;
dfs(root);
for(int j=1;j<20;j++){
for(int i=1;i<=n;i++){
up[i][j]=up[up[i][j-1]][j-1];
}
}
for(auto x:qs){
int a=e[x].f;
int b=e[x].s;
if(depth[a]>depth[b]) swap(a,b);
if(ok[x]){
ok[x]=false;
update(st[b],-1);
update(en[b]+1,1);
continue;
}
ok[x]=true;
update(st[b],1);
update(en[b]+1,-1);
int ans=0;
for(int i=19;i>=0;i--){
int tmp=ans+(1<<i);
int v=go(b,tmp);
int val=query(st[b])-query(st[v]);
if(val>=tmp){
ans=tmp;
}
}
int v=go(b,ans);
if(dp[v].size()<dp[b].size()) swap(dp[v],dp[b]);
dp[v].insert(dp[v].end(),all(dp[b]));
}
cout<<(int)dp[root].size()<<'\n';
return 0;
}
//maybe its multiset not set
//yeeorz
//laborz
Compilation message (stderr)
synchronization.cpp: In function 'void setIO(std::string)':
synchronization.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
synchronization.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |