#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N = 3e5+5;
int st[N],in[N],out[N],p[33][N],fen[N],dp[2][N],child[N],dep[N];
vector<int> A[N];
int cnt = 1;
int n,m,q,a,b,x;
vector<int> v;
void add(int pos,int val) {
for(int i = pos;i<N;i+=i&-i) fen[i]+=val;
}
int qry(int pos) {
int res = 0;
for(int i = pos;i>0;i-=i&-i) res += fen[i];
return res;
}
void dfs(int u,int par = -1) {
in[u]=cnt++;
v.push_back(u);
for(auto nxt : A[u])if(nxt!=par) {
dep[nxt] = dep[u]+1;
dfs(nxt,u);
p[0][nxt] = u;
}
out[u]=cnt;
}
void dolca() {
for(int i = 1;i<32;i++) {
for(int j = 1;j<=n;j++) p[i][j] = p[i-1][p[i-1][j]];
}
}
int fp(int x) {
for(int i = 31;i>=0;i--) {
int cur = p[i][x];
if(qry(in[x]) - qry(in[cur]) == 0) x = cur;
}
return x;
}
int main() {
v.push_back(0);
cin>>n>>m>>q;
vector<pair<int,int> > tmp;
for(int i = 0;i<n-1;i++) {
cin>>a>>b;
A[a].push_back(b);A[b].push_back(a);
tmp.push_back({a,b});
}
dfs(1);
p[0][1] = 1;
dolca();
for(int i = 0;i<n-1;i++) {
if(dep[tmp[i].f] < dep[tmp[i].s]) swap(tmp[i].f,tmp[i].s);
child[i+1] = tmp[i].f;
}
for(int i = 1;i<=n;i++) add(in[i],1),add(out[i]+1,-1),dp[0][i]=1;
// for(int i = 1;i<=n;i++)cerr<<qry(i)-qry(i-1)<< " ";
// cerr<<"\n";
for(int i = 0;i<m;i++) {
cin>>x;
x = child[x];
st[x] = 1-st[x];
if(st[x]) {
//turn on
int anc = fp(p[0][x]);
cerr << x << " " << anc << "\n";
add(in[x],-1),add(out[x]+1,1);
dp[0][anc] += dp[0][x] - dp[1][x];
} else {
// // turn off
int anc = fp(p[0][x]);
cerr << x << " " << anc << "\n";
add(in[x],1),add(out[x]+1,-1);
dp[0][x] = dp[1][x] = dp[0][anc];
}
}
for(int i = 0;i<q;i++) {
cin>>x;
if(st[x]) {
st[x] = 1-st[x];
int anc = fp(p[0][x]);
add(in[x],1),add(out[x]+1,-1);
dp[0][x] = dp[1][x] = dp[0][anc];
}
cout << dp[0][x] << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
7760 KB |
Output is correct |
2 |
Correct |
7 ms |
7760 KB |
Output is correct |
3 |
Correct |
6 ms |
7760 KB |
Output is correct |
4 |
Correct |
6 ms |
7760 KB |
Output is correct |
5 |
Incorrect |
7 ms |
7760 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1286 ms |
35224 KB |
Output is correct |
2 |
Correct |
1289 ms |
35004 KB |
Output is correct |
3 |
Correct |
753 ms |
38544 KB |
Output is correct |
4 |
Correct |
738 ms |
38588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
7752 KB |
Output is correct |
2 |
Correct |
6 ms |
7760 KB |
Output is correct |
3 |
Correct |
7 ms |
7760 KB |
Output is correct |
4 |
Correct |
7 ms |
7760 KB |
Output is correct |
5 |
Correct |
6 ms |
7760 KB |
Output is correct |
6 |
Correct |
21 ms |
8016 KB |
Output is correct |
7 |
Correct |
163 ms |
10832 KB |
Output is correct |
8 |
Correct |
1778 ms |
41428 KB |
Output is correct |
9 |
Correct |
1699 ms |
41268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1788 ms |
41372 KB |
Output is correct |
2 |
Correct |
985 ms |
39636 KB |
Output is correct |
3 |
Correct |
943 ms |
39868 KB |
Output is correct |
4 |
Correct |
956 ms |
39896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7760 KB |
Output is correct |
2 |
Incorrect |
6 ms |
7760 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |