ZOJ 4045 District Division【dfs找子节点】

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

【题意】:给一颗树,问能不能把这颗树拆成很多k个节点数相同的树,如果可以输出yes,且输出每颗树的节点,否则输出no

【总结】:在纸上多画几遍就可以发现这道题的关键点了,如果我们要把这棵树拆成n/k个节点数为k的子树,那么这棵树就必须有n/k个节点数为k的倍数的子树 于是我们dfs遍历一遍求出每课子树的节点数 然后找一下为节点数k的倍数的子树是否等于n/k 如果不是的话 就直接输出no就可以了。 如果是的话 则证明可以拆  但是我们还要把拆的结果写出来。 因此我们在判断每棵子树的节点数是否为k的倍数时还要把为k的倍数的结点标号记录下来  放到队列里  但是不能让他们同时出现在队列

 

【代码】 

#include 
using namespace std;
struct tree
{
    int n;
    vector a;
};
tree A[100005];
int B[100005];
bool vis[100005];
int findi(int i)
{
    if(!vis[i])
    {
        vis[i]=true;
        int t=1;
        for(int j=0;j T;
                T.push(B[i]);
                vis[B[i]]=true;
                while(!T.empty())
                {
                    a=T.front();
                    T.pop();
                    for(l=0;l

 

你可能感兴趣的:(dfs)