#include <bits/stdc++.h>
#define z exit(0)
#define mp make_pair
#define F first
#define S second
using namespace std;
using pii = pair<int,int>;
const int N = 1e5 + 5;
pii A[N], e[N];
vector<int> g[N];
int tin[N], tout[N], timer = 1, up[N][18], LOG, bit[N<<1];
bool active[N];
void upd(int i, int val){ for(; i<timer; i+=i&-i) bit[i] += val;}
int qry(int i){ int res = 0; for(; i; i-=i&-i) res += bit[i]; return res;}
void dfs(int u, int p){
up[u][0] = p;
tin[u] = timer++;
for(int j = 1; j<N; ++j) up[u][j] = up[up[u][j-1]][j-1];
for(int v: g[u]) if(v != p) dfs(v, u);
tout[u] = timer++;
}
int find_root(int u){
int qry_u = qry(tin[u]), v = u;
for(int j = LOG-1; j>=0; --j) if(qry(tin[up[u][j]]) == qry_u) v = up[u][j];
return v;
}
signed main(){
int n, m, q; scanf("%d %d %d", &n, &m, &q); LOG = 32 - __builtin_clz(n);
for(int i = 0, u, v; i<n-1; ++i){
scanf("%d %d", &u, &v); --u; --v;
e[i] = mp(u, v);
g[u].emplace_back(v); g[v].emplace_back(u);
}
fill(A, A+n, mp(1, 0));
dfs(0, 0);
for(int i = 0; i<n-1; ++i){
int u = e[i].F, v = e[i].S;
if(tin[u] > tin[v]) swap(u, v);
upd(tin[v], +1); upd(tout[v], -1);
}
for(int i = 0, j; i<m; ++i){
scanf("%d", &j); --j;
int u = e[j].F, v = e[j].S;
if(tin[u] > tin[v]) swap(u, v);
if(active[j]){
int root = find_root(u);
A[v] = mp(A[root].F, A[root].F);
upd(tin[v], +1); upd(tout[v], -1);
}else{
int root = find_root(u);
A[root].F += A[v].F - A[v].S;
upd(tin[v], -1); upd(tout[v], +1);
}
active[j] ^= 1;
}
while(q--){
int u; scanf("%d", &u); --u; printf("%d\n", A[find_root(u)].F);
}
}
Compilation message
synchronization.cpp: In function 'int main()':
synchronization.cpp:28:20: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | int n, m, q; scanf("%d %d %d", &n, &m, &q); LOG = 32 - __builtin_clz(n);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
synchronization.cpp:30:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | scanf("%d %d", &u, &v); --u; --v;
| ~~~~~^~~~~~~~~~~~~~~~~
synchronization.cpp:42:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | scanf("%d", &j); --j;
| ~~~~~^~~~~~~~~~
synchronization.cpp:58:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | int u; scanf("%d", &u); --u; printf("%d\n", A[find_root(u)].F);
| ~~~~~^~~~~~~~~~
synchronization.cpp: In function 'void dfs(int, int)':
synchronization.cpp:18:50: warning: iteration 18 invokes undefined behavior [-Waggressive-loop-optimizations]
18 | for(int j = 1; j<N; ++j) up[u][j] = up[up[u][j-1]][j-1];
| ~~~~~~~~~^
synchronization.cpp:18:18: note: within this loop
18 | for(int j = 1; j<N; ++j) up[u][j] = up[up[u][j-1]][j-1];
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
3 ms |
8536 KB |
Output is correct |
3 |
Incorrect |
3 ms |
8540 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
8064 ms |
14936 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
8792 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
8053 ms |
12336 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
8536 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |