Submission #680725

#TimeUsernameProblemLanguageResultExecution timeMemory
680725stevancvSynchronization (JOI13_synchronization)C++14
100 / 100
290 ms23536 KiB
#include <bits/stdc++.h> #define ll long long #define ld long double #define sp ' ' #define en '\n' #define smin(a, b) a = min(a, b) #define smax(a, b) a = max(a, b) #define sadd(a, b) a = Add(a, b) #define smul(a, b) a = Mul(a, b) using namespace std; const int N = 1e5 + 2; vector<int> g[N]; int in[N], out[N], par[N][17], tsz; int u[N], v[N], ans[N], sync[N]; bool active[N]; void Dfs(int s, int e) { in[s] = ++tsz; par[s][0] = e; for (int i = 1; i < 17; i++) par[s][i] = par[par[s][i - 1]][i - 1]; for (auto u : g[s]) { if (u == e) continue; Dfs(u, s); } out[s] = ++tsz; } int bit[2 * N]; void Add(int x, int v) { for (; x < 2 * N; x += x & (-x)) bit[x] += v; } int Get(int x) { int ans = 0; for (; x > 0; x -= x & (-x)) ans += bit[x]; return ans; } int Getroot(int x) { for (int i = 16; i >= 0; i--) { if (par[x][i] && Get(in[par[x][i]]) == Get(in[x])) x = par[x][i]; } return x; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m, q; cin >> n >> m >> q; for (int i = 1; i < n; i++) { cin >> u[i] >> v[i]; g[u[i]].push_back(v[i]); g[v[i]].push_back(u[i]); } Dfs(1, 0); for (int i = 1; i <= n; i++) { ans[i] = 1; Add(in[i], 1); Add(out[i], -1); } for (int i = 1; i < n; i++) if (in[u[i]] > in[v[i]]) swap(u[i], v[i]); while (m--) { int i; cin >> i; if (active[i] == 0) { ans[Getroot(u[i])] += ans[v[i]] - sync[v[i]]; Add(in[v[i]], -1); Add(out[v[i]], 1); } else { ans[v[i]] = sync[v[i]] = ans[Getroot(u[i])]; Add(in[v[i]], 1); Add(out[v[i]], -1); } active[i] = 1 - active[i]; } while (q--) { int x; cin >> x; cout << ans[Getroot(x)] << en; } 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...