#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
int V[200005];
int B[200005];
vector<vector<int>> C;
vector<vector<P>> adj;
struct UnionFind {
vector<int> root;
UnionFind(int N) {
root.resize(N);
fill(root.begin(),root.end(),-1);
}
int Find(int n) {
if(root[n]<0) return n;
int r = Find(root[n]);
root[n] = r;
return r;
}
void Merge(int x, int y) {
x = Find(x), y = Find(y);
if(x==y) return;
if(root[x]>root[y]) swap(x, y);
root[x] += root[y];
root[y] = x;
}
};
int Bu = 300;
int par[200005];
array<int, 2> Edge[200005];
bool type[200005]; // Edge type
bool change[200005];
int D[200005];
int id[200005];
map<int,int> son[200005];
void dfs(int c, int p) {
par[c] = p;
int dat = 0;
for(P n2 : adj[c]) {
int n = n2.first;
if(n==p) continue;
dfs(n, c);
son[c][n] = dat;
dat++;
}
C[c].resize(dat);
}
int go_par[200005];
vector<vector<P>> adj2;
void dfs2(int c, int p, int pl) {
B[c] = pl;
for(P k2 : adj[c]) {
if(k2.first!=p&&type[k2.second]) {
dfs2(k2.first, c, pl);
}
}
}
signed main() {
cin.sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N, M, Q;
cin >> N >> M >> Q;
adj.resize(N);
C.resize(N);
int i, j;
for(i=0;i<N-1;i++) {
int a, b;
cin >> a >> b;
adj[a-1].push_back(P(b-1,i));
adj[b-1].push_back(P(a-1,i));
Edge[i] = {a-1, b-1};
}
for(i=0;i<N;i++) V[i] = B[i] = 1;
for(i=0;i<M;i++) {
cin >> D[i];
D[i]--;
}
dfs(0, -1);
for(i=0;i<N;i++) go_par[i] = -1;
for(i=0;i<N-1;i++) {
if(Edge[i][0]!=par[Edge[i][1]]) {
int x = Edge[i][0];
int y = Edge[i][1];
Edge[i] = {y, x};
}
go_par[Edge[i][1]] = i;
}
int pt = 0;
for(i=0;i<M;i++) {
if(i%Bu==Bu-1||i==M-1) {
UnionFind UF(N);
for(j=pt;j<=i;j++) {
change[D[j]] = true;
}
for(j=0;j<N-1;j++) {
if(!change[j]&&type[j]) {
UF.Merge(Edge[i][0], Edge[i][1]);
}
}
for(j=0;j<N;j++) id[j] = UF.Find(j);
vector<bool> con(N);
for(j=0;j<N;j++) {
if(go_par[j]!=-1&&type[go_par[j]]) {
con[j] = true;
}
}
for(j=pt;j<=i;j++) {
if(type[D[j]]) {
type[D[j]] = false;
con[Edge[D[j]][1]] = false;
}
else {
int p = Edge[D[j]][0];
int v = Edge[D[j]][1];
int ch = son[p][v];
int pl = V[v] - C[p][ch];
int top = p;
int dat = v;
while(top != -1) {
V[top] += pl;
C[top][son[top][dat]] = V[dat];
if(!con[top]) break;
top = par[top];
}
con[v] = true;
type[D[j]] = true;
dfs2(p, -1, B[p] + pl);
}
}
for(j=pt;j<=i;j++) change[D[j]] = false;
pt = i + 1;
}
}
while(Q--) {
int k;
cin >> k;
cout << B[k-1] << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9740 KB |
Output is correct |
3 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
4228 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
958 ms |
45736 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Incorrect |
4 ms |
9684 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |