District Division ZOJ - 4045 [树+dfs]

Ezio learned a lot from his uncle Mario in Villa Auditore. He also made some contribution to Villa Auditore. One of the contribution is dividing it into many small districts for convenience of management. If one district is too big, person in control of this district would feel tiring to keep everything in order. If one district is too small, there would be too many districts, which costs more to set manager for each district.

There are  rooms numbered from 1 to  in Villa Auditore and  corridors connecting them. Let's consider each room as a node and each corridor connecting two rooms as an edge. By coincidence, Villa Auditore forms a tree.

Ezio wanted the size of each district to be exactly , which means there should be exactly  rooms in one district. Each room in one district should have at least one corridor directly connected to another room in the same district, unless there are only one room in this district (that is to say, the rooms in the same district form a connected component). It's obvious that Villa Auditore should be divided into districts.

Now Ezio was wondering whether division can be done successfully.

Input

There are multiple test cases. The first line of the input contains an integer (about 10000), indicating the number of cases. For each test case:

The first line contains two integers ,  (, ), indicating the number of rooms in Vally Auditore and the number of rooms in one district.

The following  lines each contains two integers ,  (), indicating a corrider connecting two rooms  and .

It's guaranteed that:

  •  is a multiple of ;

  • The given graph is a tree;

  • The sum of  in all test cases will not exceed .

Output

For each test case:

  • If the division can be done successfully, output "YES" (without quotes) in the first line. Then output  lines each containing  integers seperated by one space, indicating a valid division plan. If there are multiple valid answers, print any of them.

  • If the division cannot be done successfully, output "NO" (without quotes) in the first line.

Please, DO NOT output extra spaces at the end of each line, or your answer will be considered incorrect!

Sample Input

3
4 2
1 3
3 2
1 4
6 3
1 3
1 4
1 6
2 5
5 1
8 4
1 2
2 3
2 4
1 5
5 6
5 7
5 8

Sample Output

YES
1 4
2 3
NO
YES
4 3 2 1
5 6 7 8

题意:有一棵树n个点n-1条边,把这个树分为k份,每份有n/k个点,并且这n/k个点连通,如果能分输出yes,并输出每一份的点是哪些,否则输出no

思路:dfs回溯时,寻找子树大小为k的节点,这就是分成的一份,然后将改子树从树中删去(即将该子树节点个数返回0),并记录下改子树的根,当子树大小大于k时输出no。dfs完之后,当记录的树的个数不等于n/k时输出no。然后就是dfs搜索每一份的点,用vector记录一下,然后输出即可。

//这个题貌似卡memset?

#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=1000008;
int head[maxn],book[maxn],f[maxn],n,k,cnt,flag;
struct node{
	int id;
	int next;
}side[2*maxn];
vector<int> v;
vector<int> ans;
void init()
{
	for(int i=0;i<=n;i++)
	{
		head[i]=-1;
		book[i]=0;
	}
	v.clear();
	ans.clear();
	cnt=flag=0;
}
void add(int x,int y)
{
	side[cnt].id=y;
	side[cnt].next=head[x];
	head[x]=cnt++;
}
int dfs(int x,int fa)
{
	int num=1;
	for(int i=head[x];i!=-1;i=side[i].next)
	{
		int y=side[i].id;
		if(y==fa) continue;
		num+=dfs(y,x);
		if(flag) return 0;
	}
	if(num>k)
	{
		flag=1;
		return 0;
	}
	else if(num==k)
	{
		v.push_back(x);
		f[x]=fa;
		book[x]=1;
		return 0;
	}
	else return num;
}
void print(int x,int fa)
{
	ans.push_back(x);
	for(int i=head[x];i!=-1;i=side[i].next)
	{
		int y=side[i].id;
		if(y==fa||book[y]) continue;
		print(y,x);
	}
}
int main()
{
	int t,x,y;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&k);
		init();
		for(int i=1;i<=n-1;i++)
		{
			scanf("%d%d",&x,&y);
			add(x,y);
			add(y,x);
		}
		dfs(1,-1);
		if(v.size()!=n/k||flag)
			printf("NO\n");
		else
		{
			printf("YES\n");
			for(int i=0;i<v.size();i++)
			{
				ans.clear();
				print(v[i],f[v[i]]);
				book[v[i]]=1;
				for(int j=0;j<ans.size();j++)
					if(j==0)
						printf("%d",ans[j]);
					else
						printf(" %d",ans[j]);
				printf("\n");
			}
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/why932346109/article/details/88425953