标签:
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 const int N=2e5+10,M=4e6+10,inf=1e9+10,mod=1e9+7; const ll INF=1e18+10; vector<int>v[N]; int si[N],fa[N],deep[N],a[N],change[N]; int n,m,q; void init() { for(int i=0;i<=n;i++) { change[i]=0; fa[i]=0; si[i]=0; deep[i]=0; v[i].clear(); } } void dfs(int u,int pre,int dep) { deep[u]=dep; for(int i=0;i<v[u].size();i++) { if(v[u][i]==pre)continue; si[u]++;dfs(v[u][i],u,dep+1); fa[v[u][i]]=u; } } int cmp(int a,int b) { return deep[a]>deep[b]; } int main() { int T,cas=1; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&q); init(); for(int i=1;i<n;i++) { int u,w; scanf("%d%d",&u,&w); v[u].push_back(w); v[w].push_back(u); } dfs(1,0,1); printf("Case #%d:\n",cas++); while(q--) { int ans=0; scanf("%d",&m); for(int i=1;i<=m;i++) scanf("%d",&a[i]); sort(a+1,a+1+m,cmp); for(int i=1;i<=m;i++) { int FA=fa[a[i]]; if(si[a[i]]==0) { si[FA]--; change[FA]++; } if(si[a[i]]>=2) { ans++; } } printf("%d\n",n-m+ans); for(int i=1;i<=m;i++) { int FA=fa[a[i]]; if(change[FA]) { si[FA]+=change[FA]; change[FA]=0; } } } } return 0; }
标签:
原文地址:http://www.cnblogs.com/jhz033/p/5934637.html