题目链接:
Auxiliary SetTime Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 521 Accepted Submission(s): 148
An auxiliary set is a set containing vertices satisfying at least one of the two conditions:
∙It is an important vertex
∙It is the least common ancestor of two different important vertices.
You are given a tree with n vertices (1 is the root) and q queries.
Each query is a set of nodes which indicates the unimportant vertices in the tree. Answer the size (i.e. number of vertices) of the auxiliary set for each query.
For each test case, the first line contains two integers n (1≤n≤100000), q (0≤q≤100000).
In the following n -1 lines, the i-th line contains two integers ui,vi(1≤ui,vi≤n) indicating there is an edge between uii and vi in the tree.
In the next q lines, the i-th line first comes with an integer mi(1≤mi≤100000) indicating the number of vertices in the query set.Then comes with mi different integers, indicating the nodes in the query set.
It is guaranteed that ∑qi=1mi≤100000.
It is also guaranteed that the number of test cases in which n≥1000 or ∑qi=1mi≥1000 is no more than 10.
Then q lines follow, i-th line contains an integer indicating the size of the auxiliary set for each query.
#pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <bits/stdc++.h> #include <stack> #include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; typedef unsigned long long ULL; template<class T> void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar()); for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar()); F && (num=-num); } int stk[70], tp; template<class T> inline void print(T p) { if(!p) { puts("0"); return; } while(p) stk[++ tp] = p%10, p/=10; while(tp) putchar(stk[tp--] + '0'); putchar('\n'); } const LL mod=1e9+7; const double PI=acos(-1.0); const int inf=1e9; const int N=1e4+120; const int maxn=1e5+20; const double eps=1e-12; int n,q,m,son[maxn],fa[maxn],dep[maxn],SON[maxn],temp[maxn]; vector<int>ve[maxn]; void dfs(int cur,int father,int deep) { int len=ve[cur].size(); dep[cur]=deep; fa[cur]=father; if(cur!=1)son[cur]=len-1; else son[cur]=len; for(int i=0;i<len;i++) { int x=ve[cur][i]; if(x==father)continue; dfs(x,cur,deep+1); } } int cmp(int x,int y){return dep[x]>dep[y];} int main() { int t,Case=0; read(t); while(t--) { printf("Case #%d:\n",++Case); for(int i=0;i<=n;i++)ve[i].clear(); read(n);read(q); int u,v,x; for(int i=1;i<n;i++) { read(u);read(v); ve[u].push_back(v); ve[v].push_back(u); } dfs(1,0,0); while(q--) { read(m); int ans=n-m; for(int i=1;i<=m;i++)read(temp[i]),SON[temp[i]]=son[temp[i]]; sort(temp+1,temp+m+1,cmp); for(int i=1;i<=m;i++) { if(SON[temp[i]]>=2)ans++; else if(SON[temp[i]]==0) { int father=fa[temp[i]]; SON[father]--; } } printf("%d\n",ans); } } return 0; }