Link
meaning:
bombing a certain point on the tree diagram will affectthe point \ (q \) bombingsthat are not more than \ (2 \) away from the point, each time the idea of the number of damages to this point must be output : Let \ (tot [x] [0] \) bethe number of damagesto \ (x \) , \ (tot [x] [1] \) tothe number of damages to the childof \ (x \) , \ (tot [x] [2] \) The number of times the child ’s child is damagedIf the bombing \ (x \) : \ (x \) ’s father ’s father is damaged: \ (tot [fa [fa [x]]] [0 ] ++ \) \ (x \) 's father is damaged: \ (tot [fa [x]] [0] ++ \) \ (x \) and \ (x \) ' s brother is damaged: \ (tot [fa [x]] [1] ++ \) \ (x \) 's child is damaged: \ (tot [x] [1] ++ \) \ (x \) ' s child's child is damaged: \ ( tot [x] [2] ++ \) \ (x \)
The total number of damages is: \ (tot [x] [0] + tot [fa [x]] [1] + tot [fa [fa [x]]] [2] \)
Code:
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef pair<int,int> pii;
//head
const int N=750005;
vector<int>g[N];
int fa[N],tot[N][3];
void dfs(int u) {
for(auto v:g[u]) {
if(v==fa[u]) continue;
fa[v]=u;
dfs(v);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n,q;
cin>>n>>q;
for(int i=1;i<n;i++) {
int u,v;
cin>>u>>v;
g[u].pb(v);
g[v].pb(u);
}
dfs(1);
for(int i=1;i<=q;i++) {
int x;
cin>>x;
tot[x][1]++,tot[x][2]++;
if(fa[fa[x]]) tot[fa[fa[x]]][0]++;
if(fa[x]) tot[fa[x]][0]++,tot[fa[x]][1]++;
else tot[x][0]++;
cout<<tot[x][0]+tot[fa[x]][1]+tot[fa[fa[x]]][2]<<endl;
}
return 0;
}