Submission #972883

#TimeUsernameProblemLanguageResultExecution timeMemory
972883dio_2Synchronization (JOI13_synchronization)C++17
40 / 100
8069 ms16916 KiB
#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 while(parent[a] and state_kraw[ parent_kraw[a] ] ) a = parent[a]; // this is the bottle-neck. int przejdzie = info[b] - passed_cnt[e]; info[a] += przejdzie; passed_cnt[e] += przejdzie; } else { // rozlacz while(parent[a] and state_kraw[ parent_kraw[a] ] ) a = parent[a]; passed_cnt[e] = info[a]; info[b] = info[a]; } 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...