#include <bits/stdc++.h>
using namespace std;
const int N = 1E5 + 5, M = 2E5 + 5;
int n, m, q, d, u, f[N], g[N], ue[N], ve[N], lz[N], col[N];
int que[M];
bool chk[N], mod[N];
vector<int> adj[N], com[N];
void init() {
for (int i = 1; i <= n; i++) {
lz[i] = col[i] = 0;
mod[i] = false;
adj[i].clear(); com[i].clear();
}
}
void DFS(int u, int p = 0) {
col[u] = col[p];
for (int &v : adj[u]) {
int c = u ^ ue[v] ^ ve[v];
if (c != p) {
DFS(c, u);
}
}
}
void update(int u, int dif, int p = 0) {
f[u] += dif; lz[u] += dif;
for (int &v : com[u]) {
int c = u ^ col[ue[v]] ^ col[ve[v]];
if (!chk[v]) {
g[v] += dif;
} else if (c != p) {
update(c, dif, u);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> q;
fill(f + 1, f + n + 1, 1);
for (int i = 1; i < n; i++) {
cin >> ue[i] >> ve[i];
g[i] = 2;
}
for (int i = 1; i <= m; i++) {
cin >> que[i];
}
d = sqrt(3 * m);
for (int bl = 1; bl <= m; bl += d) {
init();
for (int i = 0; i < d && bl + i <= m; i++) {
mod[que[bl + i]] = true;
}
for (int i = 1; i < n; i++) {
if (!mod[i] && chk[i]) {
adj[ue[i]].push_back(i);
adj[ve[i]].push_back(i);
}
}
for (int i = 1; i <= n; i++) {
if (col[i] == 0) {
col[0] = i;
DFS(i);
}
}
for (int i = 1; i < n; i++) {
if (mod[i]) {
com[col[ue[i]]].push_back(i);
com[col[ve[i]]].push_back(i);
}
}
for (int i = 0; i < d && bl + i <= m; i++) {
int e = que[bl + i];
if (!chk[e]) {
int sum = (f[col[ue[e]]] + f[col[ve[e]]] + g[e]) / 2;
update(col[ue[e]], sum - f[col[ue[e]]]);
update(col[ve[e]], sum - f[col[ve[e]]]);
g[e] = 0;
}
chk[e] = !chk[e];
}
for (int i = 1; i <= n; i++) {
f[i] = f[col[i]];
}
for (int i = 1; i < n; i++) {
if (!mod[i] && !chk[i]) {
g[i] += lz[col[ue[i]]] + lz[col[ve[i]]];
}
}
}
while (q--) {
cin >> u;
cout << f[u] << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
5112 KB |
Output is correct |
5 |
Correct |
6 ms |
5112 KB |
Output is correct |
6 |
Correct |
8 ms |
5112 KB |
Output is correct |
7 |
Correct |
68 ms |
6012 KB |
Output is correct |
8 |
Correct |
67 ms |
6008 KB |
Output is correct |
9 |
Correct |
69 ms |
6016 KB |
Output is correct |
10 |
Correct |
2824 ms |
14388 KB |
Output is correct |
11 |
Correct |
2782 ms |
14432 KB |
Output is correct |
12 |
Correct |
1909 ms |
14660 KB |
Output is correct |
13 |
Correct |
4043 ms |
14192 KB |
Output is correct |
14 |
Correct |
2237 ms |
14560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1468 ms |
17396 KB |
Output is correct |
2 |
Correct |
1468 ms |
17172 KB |
Output is correct |
3 |
Correct |
1101 ms |
18972 KB |
Output is correct |
4 |
Correct |
1131 ms |
18900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
5112 KB |
Output is correct |
5 |
Correct |
6 ms |
5032 KB |
Output is correct |
6 |
Correct |
8 ms |
5100 KB |
Output is correct |
7 |
Correct |
52 ms |
6008 KB |
Output is correct |
8 |
Correct |
2139 ms |
14656 KB |
Output is correct |
9 |
Correct |
2401 ms |
14712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2064 ms |
14756 KB |
Output is correct |
2 |
Correct |
1122 ms |
19424 KB |
Output is correct |
3 |
Correct |
1152 ms |
19428 KB |
Output is correct |
4 |
Correct |
1137 ms |
19592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
5112 KB |
Output is correct |
5 |
Correct |
8 ms |
5112 KB |
Output is correct |
6 |
Correct |
69 ms |
6008 KB |
Output is correct |
7 |
Correct |
2935 ms |
14564 KB |
Output is correct |
8 |
Correct |
2582 ms |
14668 KB |
Output is correct |
9 |
Correct |
4235 ms |
14508 KB |
Output is correct |
10 |
Correct |
2735 ms |
15052 KB |
Output is correct |
11 |
Correct |
1559 ms |
17896 KB |
Output is correct |
12 |
Correct |
1570 ms |
17756 KB |
Output is correct |
13 |
Correct |
1184 ms |
19436 KB |
Output is correct |