This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, q;
cin >> n >> m >> q;
vector<vector<int>> g(n + 1);
vector<pair<int, int>> kraw(n);
vector<int> state_kraw(n); // na poczatky wszytko jest off.
for(int i = 0;i < n - 1;i++){
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
kraw[i + 1] = make_pair(a, b);
}
vector<int> dep(n + 1), tin(n + 1), tout(n + 1);
vector<int> parent(n + 1), parent_kraw(n + 1);
vector<int> passed_cnt(n + 1); // ile przeszlo od danej krawedzi
vector<int> info(n + 1, 1); // ile informacij ma dane korzenie ( czyli leader DSU )
int T = 1;
auto dfs1 = [&](auto &&dfs1, int u, int p)->void{
tin[u] = T++;
for(int v : g[u]) if(v != p){
parent[v] = u;
dep[v] = dep[u] + 1;
dfs1(dfs1, v, u);
}
tout[u] = T++;
}; dfs1(dfs1, 1, 0);
for(int i = 1;i <= n - 1;i++){
auto [a, b] = kraw[i];
if(dep[a] > dep[b]) swap(a, b);
parent_kraw[b] = i;
}
while(m--){
int e;
cin >> e;
auto [a, b] = kraw[e];
if(dep[a] > dep[b]) swap(a, b);
// a na gorze
if(!state_kraw[e]){ // polacz
// b jest korzeniem dolnego poddrzewa
// dla a nie wiemy kim jest leaderem treba isc do gory do poki mozna
int leader1 = b;
int leader2 = a;
while(parent[a] and state_kraw[ parent_kraw[a] ] ) a = parent[a]; // this is the bottle-neck.
leader2 = a;
// leader2 bedzie tez na koncu leader
int przejdzie = info[leader1] - passed_cnt[e];
info[leader2] += przejdzie;
passed_cnt[e] += przejdzie;
} else { // rozlacz
int leader1 = b;
int leader2 = a;
while(parent[a] and state_kraw[ parent_kraw[a] ] ) a = parent[a];
leader2 = a;
// teraz bedziemy miec leader2 i leader1
info[leader1] = info[leader2];
}
state_kraw[e] ^= 1;
}
for(int i = 0;i < q;i++){
int a;
cin >> a;
while(parent[a] and state_kraw[ parent_kraw[a] ] ) a = parent[a];
cout << info[a] << '\n';
}
return 0;
}
# | 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... |