District Division
Time Limit: 1 Second Memory Limit: 65536 KB Special Judge
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个节点,问你这个图能不能分成k个分块。这个分块内所有的节点都至少与一个该块中的其他节点有一条边相连。
思路:优先分配度为1的节点,采用深搜的方法,搜到度为一的点再回溯上去。把每个块的根节点保存下来。
具体操作注释已写在代码中
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define LL long long
#define M(a,b) memset(a,b,sizeof(a))
const int MAXN = 1e5+5;
const int INF = 0x3f3f3f3f;
int T;
int n,k;
int ai,bi;
int ans;
bool vis[MAXN];///记录i号节点是否已经被作为根节点
int num[MAXN];///记录以i号节点为根节点的块内节点数目
int hei[MAXN];///节点的高度,用于判断父亲节点和子节点
vector<int>v1[MAXN];///用于存储图的信息
vector<int>v2;///存着每个块的根节点
/*
深度优先搜索
搜到度为1的点,并将其加入其父亲节点的块中
*/
void dfs(int now,int bef)
{
hei[now] = hei[bef]+1;///记录子节点与父亲节点
if(v1[now].size()==1&&v1[now][0]==bef) return ;///搜到度为一的点(叶子节点)
int len = v1[now].size();
for(int i=0;i<len;i++)
{
if(v1[now][i]==bef) continue;///排除父亲节点
dfs(v1[now][i],now);
if(num[now]) num[now]+=num[v1[now][i]];///记录块中的节点个数
if(num[now] == k)///如果以now为根节点的块中节点数目为k
{
v2.push_back(now);///存入v2中
vis[now] = 1;///标记为1
ans++;
num[now] = 0;
}
}
return ;
}
void print(int now,int bef)
{
if(v1[now].size()==1&&v1[now][0]==bef)
{
return ;
}
int len = v1[now].size();
for(int i=0;i<len;i++)
{
if(v1[now][i]==bef)continue;
if(vis[v1[now][i]]||hei[now]>hei[v1[now][i]])continue;///优先打印子节点
vis[v1[now][i]]=1;
printf(" %d",v1[now][i]);
print(v1[now][i],now);
}
return ;
}
int main()
{
cin>>T;
while(T--)
{
M(hei,0);
M(vis,0);
v2.clear();
ans = 0;
scanf("%d %d",&n,&k);
for(int i=1;i<=n;i++)
{
num[i] = 1;///初始化
v1[i].clear();
}
for(int i=1;i<n;i++)
{
int x,y;
scanf("%d %d",&x,&y);
v1[x].push_back(y);
v1[y].push_back(x);
}
if(k==1)///k等于1和等于n的情况特殊讨论
{
printf("YES\n");
for(int i=1;i<=n;i++)
{
if(i==1) printf("%d",i);
else printf(" %d",i);
}
printf("\n");continue;
}
if(k==n)
{
printf("YES\n");
for(int i=1;i<=n;i++) printf("%d\n",i);
continue;
}
dfs(1,0);
if(ans==(n/k))///当有解的时候
{
printf("YES\n");
for(int i=0;i<v2.size();i++)
{
printf("%d",v2[i]);
print(v2[i],v2[i]);
printf("\n");
}
}
else
{
printf("NO\n");
}
}
return 0;
}