1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std;
inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; }
const int maxn=300005;
int n,q,step,First[maxn],Last[maxn],Belong[maxn],Depth[maxn],Size[maxn],Remain[maxn],p[maxn][25],b[maxn],c[maxn],Ans[maxn]; vector<int>edges[maxn],tree[maxn],a;
bool cmp(int a,int b) { return First[a]<First[b]; }
void AddEdge(int x,int y) { edges[x].push_back(y); }
void AddEdge2(int x,int y) { tree[x].push_back(y); }
void Dfs(int Now,int fa,int depth) { First[Now]=++step; Depth[Now]=depth; Size[Now]=1; p[Now][0]=fa; for(int i=1; i<=20; i++) if(~p[Now][i-1])p[Now][i]=p[p[Now][i-1]][i-1]; else break; for(int Next:edges[Now]) { if(Next==fa)continue; Dfs(Next,Now,depth+1); Size[Now]+=Size[Next]; } Last[Now]=step; }
int LCA(int a,int b) { if(Depth[a]<Depth[b])swap(a,b); for(int i=20; i>=0; i--) { if(Depth[a]==Depth[b])break; if(Depth[a]-(1<<i)>=Depth[b])a=p[a][i]; } if(a==b)return b; for(int i=20; i>=0; i--) if(p[a][i]!=-1&&p[a][i]!=p[b][i]) { a=p[a][i]; b=p[b][i]; } return p[a][0]; }
void build(vector<int>& a) { if(a.front()!=1)a.push_back(1); int tmp=a.size(); for(int i=0; i<tmp-1; i++)a.push_back(LCA(a[i],a[i+1])); sort(a.begin(),a.end(),cmp); auto it=unique(a.begin(),a.end()); a.erase(it,a.end()); static int top=0,S[maxn]; S[++top]=a[0]; for(int i=1; i<a.size(); i++) { int now=a[i]; while(top>=1) { if(First[now]>=First[S[top]]&&First[now]<=Last[S[top]]) { AddEdge2(S[top],now); break; } top--; } if(S[top]!=now)S[++top]=now; } }
int Dist(int x,int y) { return Depth[x]+Depth[y]-2*Depth[LCA(x,y)]; }
void TreeDp1(int Now) { c[++step]=Now; Remain[Now]=Size[Now]; for(int Next:tree[Now]) { TreeDp1(Next); if(!Belong[Next])continue; if(!Belong[Now]) { Belong[Now]=Belong[Next]; continue; } int d1=Dist(Belong[Next],Now),d2=Dist(Belong[Now],Now); if(d1<d2||(d1==d2&&Belong[Next]<Belong[Now]))Belong[Now]=Belong[Next]; } }
void TreeDp2(int Now) { for(int Next:tree[Now]) { if(!Belong[Next])Belong[Next]=Belong[Now]; else { int d1=Dist(Belong[Now],Next),d2=Dist(Belong[Next],Next); if(d1<d2||(d1==d2&&Belong[Now]<Belong[Next]))Belong[Next]=Belong[Now]; } TreeDp2(Next); } }
void Solve(int Now,int Next) { int son=Next,mid=Next; for(int i=20; i>=0; i--) if(Depth[p[son][i]]>Depth[Now])son=p[son][i]; Remain[Now]-=Size[son]; if(Belong[Now]==Belong[Next]) { Ans[Belong[Now]]+=Size[son]-Size[Next]; return; } for(int i=20; i>=0; i--) { int tmp=p[mid][i]; if(Depth[tmp]<=Depth[Now])continue; int d1=Dist(Belong[Now],tmp),d2=Dist(Belong[Next],tmp); if(d2<d1||(d1==d2&&Belong[Next]<Belong[Now]))mid=tmp; } Ans[Belong[Now]]+=Size[son]-Size[mid]; Ans[Belong[Next]]+=Size[mid]-Size[Next]; }
int main() { int size=40<<20; __asm__ ("movq %0,%%rsp\n"::"r"((char*)malloc(size)+size)); memset(p,-1,sizeof(p)); n=Get_Int(); for(int i=1; i<n; i++) { int x=Get_Int(),y=Get_Int(); AddEdge(x,y); AddEdge(y,x); } Dfs(1,-1,1); q=Get_Int(); for(int i=1; i<=q; i++) { int k=Get_Int(); a.clear(); for(int j=1; j<=k; j++) { int x=Get_Int(); a.push_back(x); b[j]=x; Belong[x]=x; } sort(a.begin(),a.end(),cmp); build(a); step=0; TreeDp1(1); TreeDp2(1); for(int j=1; j<=step; j++) for(int Next:tree[c[j]]) Solve(c[j],Next); for(int j=1; j<=step; j++)Ans[Belong[c[j]]]+=Remain[c[j]]; for(int j=1; j<=k; j++)printf("%d ",Ans[b[j]]); putchar('\n'); for(int j=1; j<=step; j++) { Ans[c[j]]=Belong[c[j]]=0; tree[c[j]].clear(); } } exit(0); }
|