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>
#define FOR(i, x, y) for (int i = x; i < y; i++)
#define x first
#define y second
using namespace std;
vector<int> g[100001];
pair<int, int> e[200001];
int n, m, q, f[100001], l[100001], t = 1, tin[100001], tout[100001], c[100001][20], b[100001];
void dfs(int v = 1, int p = 0) {
c[v][0] = p;
for (int i = 1; i < 20 && c[v][i - 1]; i++) c[v][i] = c[c[v][i - 1]][i - 1];
f[v] = 1;
tin[v] = t++;
for (int i : g[v]) if (i != p) dfs(i, v);
tout[v] = t;
}
void up(int p, int val) { for (; p <= n; p += (p & (-p))) b[p] += val; }
int qu(int p) {
int ans = 0;
for (; p; p -= (p & (-p))) ans += b[p];
return ans;
}
int lca(int v) {
int ll = v;
for (int i = 19; ~i; i--) if (c[ll][i] && qu(tin[c[ll][i]]) == qu(tin[v])) ll = c[ll][i];
return ll;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> q;
FOR(i, 1, n) {
cin >> e[i].x >> e[i].y;
g[e[i].x].push_back(e[i].y);
g[e[i].y].push_back(e[i].x);
}
dfs();
FOR(i, 1, n + 1) {
up(tin[i], -1);
up(tout[i], 1);
}
while (m--) {
int x;
cin >> x;
int u = e[x].x, v = e[x].y;
if (c[u][0] == v) swap(u, v);
if (lca(v) != v) {
f[v] = l[v] = f[lca(u)];
up(tin[v], -1);
up(tout[v], 1);
} else {
f[lca(u)] += f[v] - l[v];
up(tin[v], 1);
up(tout[v], -1);
}
}
while (q--) {
int x;
cin >> x;
cout << f[lca(x)] << '\n';
}
return 0;
}
# | 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... |