###### （HDU 5927）Auxiliary Set 思维题 <2016CCPC东北地区大学生程序设计竞赛 - 重现赛 >

Auxiliary Set
Problem 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.

Input
The first line contains only one integer T (T≤1000), which indicates the number of test cases.

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.

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.

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

1:是重要节点
2:是两个重要节点的最近公共祖先

AC代码：

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 100010;
struct egde
{
int u,v,next;
}edges[2*maxn];
int n,m,q,e;
int par[maxn],son[maxn],tson[maxn],dep[maxn],qs[maxn];
bool vis[maxn];

{
edges[e].u = u;
edges[e].v = v;
}

void dfs(int u,int fa,int d)
{
vis[u] = true; par[u] = fa; dep[u] = d;
{
if(!vis[edges[i].v])
{
dfs(edges[i].v,u,d+1);
son[u]++;
}
}
}

int cmp(int a,int b)
{
return dep[a] > dep[b];
}

int main()
{
int t,u,v;
int kase=1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&q);
e = 0;
for(int i=1;i<n;i++)
{
scanf("%d%d",&u,&v);
}
memset(vis,false,sizeof(vis));
memset(son,0,sizeof(son));
dfs(1,-1,1);
printf("Case #%d:\n", kase++);
while(q--)
{
scanf("%d",&m);
for(int i=1;i<=m;i++) scanf("%d",&qs[i]);
for(int i=1;i<=m;i++) tson[qs[i]] = son[qs[i]];
sort(qs+1,qs+m+1,cmp);
int ans = n - m;
for(int i=1;i<=m;i++)
{
if(tson[qs[i]] >= 2) ans++;
else if(tson[qs[i]] == 0) tson[par[qs[i]]]--;
}
printf("%d\n",ans);
}
}
return 0;
}


#### F - Auxiliary Set HDU - 5927

2017-02-16 09:34:59

#### HDU 5927 Auxiliary Set LCA 2016东北4省赛

2016-11-08 22:41:26

#### Codeforces Round #411 (Div. 1) D. Expected diameter of a tree(树的直径)

2017-05-09 22:37:35

#### 902B - Coloring a Tree

2017-12-22 17:44:24

#### Codeforces Round #453 (Div. 2)

2017-12-20 23:00:40

#### hdu 5946 Fxx and tree

2017-03-19 21:26:01

#### NYOJ 47 过河问题 详解思路

2016-08-08 10:01:49

#### 算法分析--最优分解问题

2017-11-28 09:07:00

#### 并查集算法解决Redundant Connection I&II

2017-10-03 00:14:07

#### hdu 5927 树形dp

2017-07-12 17:57:05

## 不良信息举报

（HDU 5927）Auxiliary Set 思维题 <2016CCPC东北地区大学生程序设计竞赛 - 重现赛 >