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;
#define task ""
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int MAX = 100010;
int n, m, q;
int timer;
int tin[MAX];
int bit[MAX];
int tout[MAX];
bool active[MAX];
int anc[17][MAX];
int a[MAX], c[MAX];
vector<int> adj[MAX];
pair<int, int> edges[MAX];
void update(int id, int delta) {
for (; id <= n; id += id & -id)
bit[id] += delta;
}
int get(int id) {
int res = 0;
for (; id > 0; id -= id & -id)
res += bit[id];
return res;
}
void dfs(int u, int p) {
if (p != -1) {
for (int j = 1; j <= 16; ++j) anc[j][u] = anc[j - 1][anc[j - 1][u]];
}
tin[u] = ++timer;
for (int v: adj[u]) if (v != p) {
anc[0][v] = u;
dfs(v, u);
}
tout[u] = timer;
}
int root(int u) {
int v = u;
for (int j = 16; j >= 0; --j) if (anc[j][v] > 0) {
if (get(tin[u]) == get(tin[anc[j][v]]))
v = anc[j][v];
}
return v;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin >> n >> m >> q;
for (int i = 1; i <= n - 1; ++i) {
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
edges[i] = {u, v};
}
dfs(1, -1);
for (int i = 1; i <= n; ++i) {
a[i] = 1;
update(tin[i], +1);
update(tout[i] + 1, -1);
}
while (m--) {
int j; cin >> j;
int u, v; tie(u, v) = edges[j];
if (anc[0][u] == v) swap(u, v);
if (active[v]) {
a[v] = c[v] = a[root(u)];
update(tin[v], +1);
update(tout[v] + 1, -1);
} else {
a[root(u)] += a[v] - c[v];
update(tin[v], -1);
update(tout[v] + 1, +1);
}
active[v] ^= 1;
}
while (q--) {
int u; cin >> u;
cout << a[root(u)] << '\n';
}
return 0;
}
Compilation message (stderr)
synchronization.cpp: In function 'int main()':
synchronization.cpp:56:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
56 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
synchronization.cpp:57:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |