[Full] k-tree Perfect Tree

Title Description

Given a positive integer k, we define a rooted tree to be k-perfect, if and only if it meets both conditions below:
•Each node is either a leaf node or having exactly k direct offsprings.
•All leaf nodes have the same distance to the root (i.e., all leaf nodes are of the same depth).
Now you are given an unrooted tree, and you should answer these questions:
•Is it possible to assign it a root, so that the tree becomes k-perfect for some positive integer k?
•If possible, what is the minimal k?
 

Entry

Read from the standard input.
Each input contains multiple test cases.
The first line contains a single positive integer T, indicating the number of test cases.
For each test case, its first line contains a positive integer n, describing the number of tree nodes. Each of the next n − 1 lines contains two space-separated integers u and v, which means there exists an edge between node u and v on the tree.
It is guaranteed each test case gives a valid unrooted tree, and the nodes are numbered with consecutive integers from 1 to n.
The sum of n in each input will not exceed 1e6.
 

Export

Write to the standard output.
For each test case, output a single integer in a line:
•If the answer to the first question is "No", output −1.
•Otherwise, output the minimal k.
 

Sample input

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

Sample Output

2
-1



[Title] Italy

  Analysis n points, n-1 edges of unrooted trees. Does Zheke unrooted trees tree is full k, if k is a full binary tree, output a k. Otherwise, output "-1."

[Feel]

  Too much detail, really too much, WA29 times .......

  Finally I saw someone else's code to know where he was wrong.

  

  The observable characteristics of the tree:

  The case of the following circumstances:

  Degree 1: leaf node

       Of degree k: root (while only one)

  Of degree k + 1: the other nodes.

 

  Special cases:

  1, FIG chrysanthemum, a node is attached to a plurality of leaf nodes

  2, single chain

  3, full K-ary tree

  4, or do not meet.


 

 

  1 #pragma GCC optimize(2)
  2 #include<bits/stdc++.h>
  3 using namespace std;
  4 const int N = 2e6+10;
  5 const int inf = 0x3f3f3f3f ;
  6 
  7 /*——————————————————Add_edge()————————————————*/
  8 
  9 typedef struct Edge{
 10     int to , next ;
 11 }Edge ;
 12 Edge e[N<<1];
 13 int head[N] , cnt ;
 14 
 15 void Add_edge( int u , int v ){
 16     e[cnt] = Edge{ v ,head[u] };
 17     head[u] = cnt ++ ;
 18 }
 19 
 20 /*——————If you ask me how much i love you ———————*/
 21 
 22 int vis[N],du[N],dep[N],Num[N],n;
 23 int p[N];
 24 void Init(){
 25     for(int i=0;i<=n;i++){
 26         head[i] = -1 ;
 27         Num[i] = dep[i] = p[i] = vis[i] = du[i] = 0 ;
 28     }
 29     cnt = 0 ;
 30 }
 31 int main()
 32 {
 33     int T;
 34     scanf("%d",&T);
 35     while(T--){
 36         
 37         scanf("%d",&n);
 38         Init();
 39 
 40         for( int i = 1, U, V; I <n-; I ++ ) {
 41 is              Scanf ( " % D% D " , & U, & V);
 42 is              add_edge (U, V);
 43 is              add_edge (V, U);
 44 is              du [U] + +; du [V] ++ ;
 45          }
 46 is          // number of direct determination node <= 3 are 1. 
47          IF (n-<= 3 ) {           
 48              the printf ( " . 1 \ n- " );
 49              Continue ;
 50          }
 51 is  
52 is          BOOL F = to true;
 53 is  
54 is          int TOT = 0 ;
 55          for ( int U = . 1 ; U <= n-; U ++ ) {
 56 is              IF (VIS [du [U]] == 0 )
 57 is                  P [TOT ++] = du [U] ;
 58              VIS [du [U]] ++ ;
 59          }
 60  
61 is          Sort (P, P + TOT);
 62 is          int K = P [ . 1 ];
 63 is          
64          // single chain case 
65          IF (TOT == 2 && VIS [. 1 ] == 2 ) { 
 66              the printf ( " . 1 \ n- " );       Continue ;
 67          }
 68          // case having one of 
69          the else  IF (TOT == 2 && VIS [ . 1 ] == n-- . 1 ) {    
 70              the printf ( " % D \ n- " , P [ . 1 ]); Continue ;
 71 is          }
 72          // only one of degree k, and the number of the third must be. 1 + K 
73 is          the else  IF (TOT == 3 ){
 74             if( vis[p[1]] != 1 || p[1] != p[2] - 1 ){   
 75                 f = false ;
 76             }else{
 77                 int root = 0 , Max_dep = 0 ;
 78                 for(int u = 1 ; u <= n ; u++ ){
 79                     if( du[u] == p[1] ){
 80                         root = u ;
 81                         break;
 82                      }
 83                  }
 84              / *   use of full depth to determine whether K-ary tree       * / 
85                  Queue < int > Q;
 86                  Q.push (the root);
 87  
88                  DEP [the root] = 0 ;
 89                  the Num [ 0 ] + + ;
 90                  
91 is                  the while (! {Q.empty ())
 92                      int U = Q.front ();
 93                      Q.pop ();
 94                      du [U] = - . 1 ;
 95                     for(int i = head[u] ; ~i ; i = e[i].next ){
 96                         int To = e[i].to;
 97                         if( du[To] == -1 ) continue ;
 98                         dep[To] = dep[u] + 1 ;
 99                         Num[dep[To]] ++ ;
100                         Max_dep = max( dep[To] , Max_dep );
101                         Q.push(To);
102 
103                     }
104                 }
105 
106                 for(int i=0;i<Max_dep;i++){
107                     if( Num[i]*k != Num[i+1] ) f = false ;
108                 }
109             }
110         }else{
111             f = false ;
112         }
113         
114         if( f ){
115             printf("%d\n",k);
116         }else{
117             printf("-1\n");
118         }
119     }
120     return 0 ;
121 }
122 
123 /*
124 10
125 7
126 1 4
127 4 5
128 3 1
129 2 3
130 7 3
131 4 6
132 7
133 1 4
134 1 5
135 1 2
136 5 3
137 5 6
138 5 7
139 13
140 1 2
141 1 3
142 1 4
143 2 5
144 2 6
145 2 7
146 3 8
147 3 9
148 3 10
149 5 11
150 5 12
151 5 13
152 5
153 1 2
154 2 3
155 3 4
156 4 5
157 
158 */
View Code

 

Guess you like

Origin www.cnblogs.com/Osea/p/11441420.html