hdu 5927 ( 思维 + 树 )

Auxiliary Set

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


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  uii 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
 
 
16 36 42 55 41 55 33 1 2 31 53 3 1 4
 

Sample Output
 
 
Case #1:363
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
 

Source

题意:  给你一颗树 ,  然后每次询问给你c 个点为不重要的点,然后你要做的就是求出某个特定集合的 点的个数。 一个点若想在这个集合内,那么他就要满足两个条件中的一个,  第一 是重要点  二  是两个非重要的点的最近公共祖先。

思路 :  我们先dfs 一遍建出一棵树 记录每个节点的儿子个数。   然后对于每次询问,我们需要对不重要的点进行单独建树,然后遍历每一棵树,求出树上满足是两非重要点的个数。  

代码: 

#include<bits/stdc++.h>
#define N 100005

using namespace std;

int head[N];
struct node
{
	int v;
	int next;
}edge[2*N];

int tot;
map<int ,int >mp;
vector<int >ve[N];
int n,q;
int num[N];
int sum[N];
int fa[N];
int ans;

void add(int u,int v){
	edge[++tot].v=v;
	edge[tot].next=head[u];
	head[u]=tot;
	return ;
}

void dfs(int u,int f){
	fa[u]=f;
	for(int i=head[u];i!=-1;i=edge[i].next){
		int v=edge[i].v;
		if(v==f) continue;
		dfs(v,u);
		sum[u]++;
	}
	return ;
}

int dfs1(int u,int f)
{
	if(mp[u]!=-1) return mp[u]; 
	
	int ssum=0;
	int son=0;
	int sz=ve[u].size();
	for(int i=0;i<sz;i++){
		int v=ve[u][i];
		if(v==f) continue;
		
		if(dfs1(v,u)>=1) ssum++;
		son++;
	}
	
	if(sum[u]-son+ssum>=2){
		ans++;
		return mp[u]=2;
	}
	if(sum[u]-son+ssum>=1){
		return mp[u]=1;
	}
	return mp[u]=-2;
}

int main()
{
	int T;
	int u,v;
	int kk=0;
	scanf("%d",&T);
	while(T--){
		memset(head,-1,sizeof(head));
		memset(sum,0,sizeof(sum));
		tot=0;
		
		scanf("%d %d",&n,&q);
		
		for(int i=1;i<n;i++){
			scanf("%d %d",&u,&v);
			add(u,v);
			add(v,u);
		}
		
		dfs(1,-1);
		/*
		for(int i=1;i<=n;i++) printf("%d ",sum[i]);
		printf("\n"); 
		*/
		
		
		printf("Case #%d:\n",++kk);
		while(q--){
			int cc;
			scanf("%d",&cc);
			mp.clear();
			for(int i=1;i<=cc;i++){
				scanf("%d",&num[i]);
				mp[num[i]]=-1;
				ve[num[i]].clear();
			}
			
			for(int i=1;i<=cc;i++){
				int f=fa[num[i]];
				if(mp[f]==-1){
					ve[f].push_back(num[i]);
				}
			}
			ans=0;
			for(int i=1;i<=cc;i++){
				if(mp[num[i]]==-1){
					dfs1(num[i],num[i]);
				}
			}
			
			ans=n-cc+ans;
			printf("%d\n",ans);
		}
		
	}
	return 0;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值