Submission #531073

#TimeUsernameProblemLanguageResultExecution timeMemory
531073Alex_tz307Synchronization (JOI13_synchronization)C++17
100 / 100
238 ms24784 KiB
#include <bits/stdc++.h> using namespace std; const int kN = 1e5; const int kLog = 16; int n, m, q, timer, tin[1 + kN], tout[1 + kN], aib[1 + kN], card[1 + kN], last[1 + kN], anc[1 + kN][1 + kLog]; short act[1 + kN]; vector<int> g[1 + kN]; pair<int, int> edges[1 + kN]; void dfs(int u) { tin[u] = ++timer; card[u] = 1; act[u] = -1; for (int i = 1; i <= kLog; ++i) { anc[u][i] = anc[anc[u][i - 1]][i - 1]; if (anc[u][i] == 0) { break; } } for (int v : g[u]) { if (v != anc[u][0]) { anc[v][0] = u; dfs(v); } } tout[u] = timer; } void update(int x, int v) { for (int i = x; i <= n; i += i & -i) { aib[i] += v; } } int query(int x) { int ans = 0; for (int i = x; i > 0; i = i & (i - 1)) { ans += aib[i]; } return ans; } int findRoot(int v) { for (int i = kLog; i >= 0; --i) { if (anc[v][i] && query(tin[v]) == query(tin[anc[v][i]])) { v = anc[v][i]; } } return v; } void testCase() { cin >> n >> m >> q; for (int i = 1; i < n; ++i) { int u, v; cin >> u >> v; g[u].emplace_back(v); g[v].emplace_back(u); edges[i] = {u, v}; } dfs(1); for (int v = 2; v <= n; ++v) { update(tin[v], act[v]); update(tout[v] + 1, -act[v]); } for (int i = 0; i < m; ++i) { int d; cin >> d; int u, v; tie(u, v) = edges[d]; if (anc[u][0] == v) { swap(u, v); } act[v] *= -1; if (act[v] == 1) { int rootU = findRoot(u); card[rootU] += card[v] - last[v]; } else { card[v] = card[findRoot(u)]; last[v] = card[v]; } update(tin[v], act[v]); update(tout[v] + 1, -act[v]); } for (int i = 0; i < q; ++i) { int v; cin >> v; cout << card[findRoot(v)] << '\n'; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int tests = 1; for (int tc = 0; tc < tests; ++tc) { testCase(); } 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...