This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// cre: Nguyen Ngoc Hung - Train VOI 2024 :>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <unordered_set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <cstring>
#include <unordered_map>
#include <cmath>
#include <array>
#include <cassert>
#include <random>
using namespace std;
#define __nnhzzz__ signed main()
#define BIT(i,j) ((i>>j)&1LL)
#define MASK(i) (1LL<<i)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) (int)(x).size()
#define fi first
#define se second
#define ll long long
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vpii vector<pii>
#define REPDIS(i,be,en,j) for(int i = (be); i<=(en); i+=j)
#define REPD(i,be,en) for(int i = (be); i>=(en); i--)
#define REP(i,be,en) for(int i = (be); i<=(en); i++)
//-------------------------------------------------------------//
const int oo = 1e9,LOG = 17,MAXN = 1e5+7,N = 1e2+3;
const int MOD = 1e9+7,MOD1 = 1e9+207,MOD2 = 1e9+407,MOD3 = 998244353;
//-------------------------------------------------------------//
template<typename T1, typename T2> bool mini(T1 &a, T2 b){if(a>b){a=b;return true;}return false;}
template<typename T1, typename T2> bool maxi(T1 &a, T2 b){if(a<b){a=b;return true;}return false;}
template<typename T> T gcd(T a, T b) { while(b) { a %= b; swap(a,b); } return a; }
template<typename T> T lcm(T a, T b) { return a/gcd(a,b)*b; }
/*
----------------------------------------------------------------
END OF TEMPLATE
----------------------------------------------------------------
Nguyen Ngoc Hung - nnhzzz
Training for VOI24 gold medal
----------------------------------------------------------------
*/
struct FENWICK_TREE{
vi f;
int n;
void update(int id, int val){
for(; id<=n; id+=id&-id){
f[id] += val;
}
}
int get(int id){
int res = 0;
for(; id>0; id-=id&-id){
res += f[id];
}
return res;
}
FENWICK_TREE(int _ = 0):n(_){
f.assign(n+1,0);
}
}fen;
int tin[MAXN],tout[MAXN],f[MAXN][LOG],lst[MAXN],s[MAXN],ok[MAXN];
pii edge[MAXN];
vi adj[MAXN];
int timer = 0;
void dfs(int u, int par){
REP(i,1,LOG-1) f[u][i] = f[f[u][i-1]][i-1];
tin[u] = ++timer;
for(const auto &v:adj[u]){
if(v==par) continue;
f[v][0] = u;
dfs(v,u);
}
tout[u] = timer;
}
int find(int u){
int lca = u,val = fen.get(tin[u]);
REPD(i,LOG-1,0){
if(f[lca][i]!=0 && fen.get(tin[f[lca][i]])==val) lca = f[lca][i];
}
return lca;
}
void solve(){
int cur = 0,n,m,q; cin >> n >> m >> q;
REP(i,1,n-1){
auto &[u,v] = edge[i]; cin >> u >> v;
// cerr << u << " " << v << endl;
adj[u].push_back(v);
adj[v].push_back(u);
}
REP(i,1,n) s[i] = 1;
dfs(1,0);
fen = FENWICK_TREE(n+1);
REP(i,1,n){
fen.update(tin[i],-1);
fen.update(tout[i]+1,1);
}
while(m--){
int pos; cin >> pos;
auto [u,v] = edge[pos];
if(f[u][0]==v) swap(u,v);
if(ok[pos]==0){
s[find(u)] += s[v]-lst[v];
fen.update(tin[v],1);
fen.update(tout[v]+1,-1);
}else{
s[v] = lst[v] = s[find(u)];
fen.update(tin[v],-1);
fen.update(tout[v]+1,1);
}
ok[pos] ^= 1;
}
while(q--){
int pos; cin >> pos;
cout << s[find(pos)] << "\n";
}
}
__nnhzzz__{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define name "test"
if(fopen(name".inp","r")){
freopen(name".inp","r",stdin);
freopen(name".out","w",stdout);
}
#define name1 "disconnect"
if(fopen(name1".in","r")){
freopen(name1".in","r",stdin);
freopen(name1".out","w",stdout);
}
int test = 1;
while(test--){
solve();
}
cerr << "\nTime elapsed: " << 1000*clock()/CLOCKS_PER_SEC << "ms\n";
return 0;
}
Compilation message (stderr)
synchronization.cpp: In function 'void solve()':
synchronization.cpp:128:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
128 | auto &[u,v] = edge[i]; cin >> u >> v;
| ^
synchronization.cpp:142:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
142 | auto [u,v] = edge[pos];
| ^
synchronization.cpp:126:9: warning: unused variable 'cur' [-Wunused-variable]
126 | int cur = 0,n,m,q; cin >> n >> m >> q;
| ^~~
synchronization.cpp: In function 'int main()':
synchronization.cpp:166:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
166 | freopen(name".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
synchronization.cpp:167:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
167 | freopen(name".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
synchronization.cpp:171:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
171 | freopen(name1".in","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
synchronization.cpp:172:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
172 | freopen(name1".out","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... |