zoj 4045 District Division
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
Author: ZHANG, Yuan
Source: ZOJ Monthly, June 2018
题意 : 给一棵树,要求将该树进行分区,每个区要求有 k 个节点, 问是否能按要求分区,如果可以,将每个区中的元素输出。
思路 : 由题意可推出,只有连接的节点个数为 k 的倍数的点可以作为根节点,并且根节点个数必须等于 n/k 。
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
int u,v,tmp[maxn],sz[maxn];
int fa[maxn];
bool vis[maxn];
int n,T,k,c;
vector<int>color[maxn],e[maxn];
void init(){
for (int i=1; i<=n ;i++){
vis[i]=false;
tmp[i]=0;
color[i].clear();
e[i].clear();
}
c=0;
}
void dfs_1(int x,int p){
sz[x]=1;
for(auto v:e[x]){
if(p!=v){
dfs_1(v,x);
sz[x]+=sz[v];
}
}
tmp[sz[x]]++;
fa[x]=p;
if(sz[x]%k==0) color[sz[x]].push_back(x);
}
void dfs_2(int x,int p){
if(vis[x]) return ;
vis[x]=1;
++c;
printf("%d%c",x,c%k==0?'\n':' ');
for (auto v:e[x]){
if(v!=p) dfs_2(v,x);
}
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&k);
init();
for (int i=1; i<n ;i++) {
scanf("%d%d",&u,&v);
e[u].push_back(v);
e[v].push_back(u);
}
dfs_1(1,1);
int ct=0;
for (int i=k; i<=n; i+=k)
ct+=tmp[i];
if(ct!=n/k){
puts("NO");
continue;
}
printf("YES\n");
for (int i=k;i<=n; i+=k){
for (auto u:color[i]){
dfs_2(u,fa[u]);
}
}
}
return 0;
}