Auxiliary Set(dfs求每个节点的儿子个数)

Auxiliary Set

Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 491    Accepted Submission(s): 136


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 ( T1000 ), which indicates the number of test cases.

For each test case, the first line contains two integers n ( 1n100000 ), q ( 0q100000 ).

In the following n -1 lines, the i-th line contains two integers  ui,vi(1ui,vin)  indicating there is an edge between  ui i and  vi  in the tree.

In the next q lines, the i-th line first comes with an integer  mi(1mi100000)  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=1mi100000 .

It is also guaranteed that the number of test cases in which  n1000   or  qi=1mi1000  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
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

用dfs先求出所有点的儿子树和父亲节点,分别记录在son[]和fa[]数组中,我们要找不重要节点中能加入集合中的点,把不重要的点按深度由大到小排序后遍历,如果该点son[]大于2,那么这个点可以加入集合,如果这个点的son为0 那么son[fa[i]]-1(i表示这个点),这个比较巧妙,可以弄几个样例模拟下。

答案就出来了。

#include<iostream>
#include<string.h>
#include<algorithm>
#include<cstdio>
#include<vector>
using namespace std;
int level[100005];
int son[100005];
vector<int>V[100005];
int a[100005];
int child[100005];
int fa[100005];
void dfs(int root)
{
	son[root]=0;
	for(int i=0;i<V[root].size();i++)
	{
		if(level[V[root][i]]==0)
		{
		  fa[V[root][i]]=root;
		  level[V[root][i]]=level[root]+1;
		  dfs(V[root][i]);
		  son[root]++;
		}
	}
}
bool cmp(int a,int b)
{
	return level[a]>level[b];
}
int main()
{
	int t;
	scanf("%d",&t);
	int nn=0;
	while(t--)
	{
		printf("Case #%d:\n",++nn);
		memset(level,0,sizeof(level));
		level[1]=1;
		int n,q;
		scanf("%d%d",&n,&q);
		int u,v;
		for(int i=0;i<=n;i++)
		{
			V[i].clear();
		}
		for(int i=0;i<n-1;i++)
		{
			scanf("%d%d",&u,&v);
			V[u].push_back(v);
			V[v].push_back(u);
		}
		fa[1]=0;
		dfs(1);
		for(int i=0;i<q;i++)
		{
			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++)
			{
				child[a[i]]=son[a[i]];
			}
			for(int i=0;i<num;i++)
			{
				if(child[a[i]]>=2)
				{
					ans++;
				}
				else if(child[a[i]]==0)
				{
					child[fa[a[i]]]--;
				}
			}
			printf("%d\n",ans);
		}
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值