思路:考虑对于每一个询问的点按照深度排序,如果该不重要的点的儿子有两个,那么就证明可以贡献一个答案,如果该不重要的点没有儿子了,那么对于该点的父节点的儿子数减一
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100000+7;
vector<int>e[maxn];
int dep[maxn],fa[maxn],son[maxn];
int a[maxn];
int b[maxn];
void dfs(int u,int f,int d)
{
dep[u]=d;
fa[u]=f;
int len = e[u].size();
for(int i= 0;i<len;i++)
{
int v = e[u][i];
if(v==f)continue;
dfs(v,u,d+1);
son[u]++;
}
}
bool cmp(int a,int b)
{
return dep[a]>dep[b];
}
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
printf("Case #%d:\n",cas++);
int n,q;
scanf("%d%d",&n,&q);
for(int i =0;i<=n;i++)e[i].clear();
memset(son,0,sizeof(son));
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(int i = 1;i<=n-1;i++)
{
int u,v;
scanf("%d%d",&u,&v);
e[u].push_back(v);
e[v].push_back(u);
}
dfs(1,-1,0);
while(q--)
{
int num;
scanf("%d",&num);
int ans = n-num;
for(int i = 0;i<num;i++)
scanf("%d",&a[i]);
sort(a,a+num,cmp);
for(int i = 0;i<num;i++)b[a[i]]=son[a[i]];
for(int i = 0;i<num;i++)
{
if(b[a[i]]>=2)ans++;
else if (b[a[i]]==0)b[fa[a[i]]]--;
}
printf("%d\n",ans);
}
}
}
Description
Given a rooted tree with n vertices, some of the vertices are important.
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.
An auxiliary set is a set containing vertices satisfying at least one of the two conditions:


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.
Input
The first line contains only one integer T (




),
which indicates the number of test cases.
For each test case, the first line contains two integers n (








),
q (








).
In the following n -1 lines, the i-th line contains two integers














indicating
there is an edge between 
i
and 
in
the tree.
In the next q lines, the i-th line first comes with an integer













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












.
It is also guaranteed that the number of test cases in which




or 










is
no more than 10.






For each test case, the first line contains two integers n (




















In the following n -1 lines, the i-th line contains two integers




















In the next q lines, the i-th line first comes with an integer















It is guaranteed that














It is also guaranteed that the number of test cases in which


















Output
For each test case, first output one line "Case #x:", where x is the case number (starting from 1).
Then q lines follow, i-th line contains an integer indicating the size of the auxiliary set for each query.
Then q lines follow, i-th line contains an integer indicating the size of the auxiliary set for each query.
Sample Input
1 6 3 6 4 2 5 5 4 1 5 5 3 3 1 2 3 1 5 3 3 1 4
Sample Output
Case #1:
3
6
3
Hint
For the query {1,2, 3}: •node 4, 5, 6 are important nodes For the query {5}: •node 1,2, 3, 4, 6 are important nodes •node 5 is the lea of node 4 and node 3 For the query {3, 1,4}: • node 2, 5, 6 are important nodes