HDU 5927 Auxiliary Set(树上搞事)

本文介绍了一种解决树形结构中辅助集大小查询问题的算法。通过深度优先搜索(DFS)预处理树的深度及子节点数量,再对每个查询进行排序处理,最终计算出在去除指定不重要节点后的辅助集大小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:考虑对于每一个询问的点按照深度排序,如果该不重要的点的儿子有两个,那么就证明可以贡献一个答案,如果该不重要的点没有儿子了,那么对于该点的父节点的儿子数减一


#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. 

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. 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值