题目描述

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?

 

输入

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.

 

输出

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.

 

样例输入

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

样例输出

2
-1

【题意】

  给出n个点,n-1条边的无根树,请问这颗无根树是否为满k叉树,如果是满k叉树,请输出k。否则输出"-1".

【感受】

  太多细节了,真的太多太多了,WA29次.......

  最后还是看了别人的代码才知道自己错哪里了。

  

  根据树的特点观察得到:

  度的情况有以下情况:

  度数为1 :叶子节点

度数为k :根节点  (同时只有1个)

  度数为k+1 :其他节点。

  特例:

  1、菊花图,就是一个节点连着多个叶子节点

  2、单链

  3、满K叉树

  4、否则都不满足。


 #pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
const int N = 2e6+;
const int inf = 0x3f3f3f3f ; /*——————————————————Add_edge()————————————————*/ typedef struct Edge{
int to , next ;
}Edge ;
Edge e[N<<];
int head[N] , cnt ; void Add_edge( int u , int v ){
e[cnt] = Edge{ v ,head[u] };
head[u] = cnt ++ ;
} /*——————If you ask me how much i love you ———————*/ int vis[N],du[N],dep[N],Num[N],n;
int p[N];
void Init(){
for(int i=;i<=n;i++){
head[i] = - ;
Num[i] = dep[i] = p[i] = vis[i] = du[i] = ;
}
cnt = ;
}
int main()
{
int T;
scanf("%d",&T);
while(T--){ scanf("%d",&n);
Init(); for( int i = ,u,v ; i < n ; i++ ){
scanf("%d%d",&u,&v);
Add_edge( u , v );
Add_edge( v , u );
du[u] ++ ; du[v] ++ ;
}
//直接判断节点个数<=3时都为1.
if( n <= ){
printf("1\n");
continue ;
} bool f = true; int tot = ;
for(int u = ; u <= n ; u ++ ){
if( vis[du[u]] == )
p[tot++] = du[u] ;
vis[du[u]] ++ ;
} sort( p , p + tot );
int k = p[] ; //单链情况
if( tot == && vis[] == ){
printf("1\n"); continue ;
}
//具有一层的情况
else if( tot == && vis[] == n - ){
printf("%d\n",p[]) ; continue ;
}
// 度数为k的只有一个,而且第三种度的个数一定是K+1
else if( tot == ){
if( vis[p[]] != || p[] != p[] - ){
f = false ;
}else{
int root = , Max_dep = ;
for(int u = ; u <= n ; u++ ){
if( du[u] == p[] ){
root = u ;
break ;
}
}
/* 利用深度来判断是否为满K叉树 */
queue< int > Q ;
Q.push( root ); dep[root] = ;
Num[] ++ ; while( !Q.empty() ){
int u = Q.front() ;
Q.pop();
du[u] = - ;
for(int i = head[u] ; ~i ; i = e[i].next ){
int To = e[i].to;
if( du[To] == - ) continue ;
dep[To] = dep[u] + ;
Num[dep[To]] ++ ;
Max_dep = max( dep[To] , Max_dep );
Q.push(To); }
} for(int i=;i<Max_dep;i++){
if( Num[i]*k != Num[i+] ) f = false ;
}
}
}else{
f = false ;
} if( f ){
printf("%d\n",k);
}else{
printf("-1\n");
}
}
return ;
} /*
10
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
13
1 2
1 3
1 4
2 5
2 6
2 7
3 8
3 9
3 10
5 11
5 12
5 13
5
1 2
2 3
3 4
4 5 */

最新文章

  1. 【leedcode】longest-substring-without-repeating-characters
  2. 基于Centos7+Nginx+Tomcat8的负载均衡服务器的搭建
  3. 锁&amp;锁与指令原子操作的关系 &amp; cas_Queue
  4. 新手学JavaScript都要学什么?
  5. maven 添加支持编译jdk1.7
  6. UI1_UICollectionView
  7. easyui源码翻译1.32--ComboTree(树形下拉框)
  8. HDU2024JAVA
  9. 修改Fedora 20 启动项
  10. Android蓝牙A2dp profile的使用
  11. 走进windows编程的世界-----windows进程
  12. DataLoad命令
  13. Linux新手随手笔记1.4
  14. Shell编程(week4_day2)--技术流ken
  15. MyBatis联表查询
  16. sf-1 算法
  17. [Python设计模式] 第19章 分公司=部门?——组合模式
  18. CRC---循环冗余校验
  19. django 初始化 介绍 生命周期
  20. 接收键盘输入的字符串,用FileWirter类将字符串写入文件,用FileReader类读出文件内容显示在屏幕上

热门文章

  1. mysql远程访问设置
  2. python人生如初见之初见yield
  3. python 设计模式之享元(Flyweight)模式
  4. Mat转CImage
  5. fbx模型在OSG中渲染
  6. 41 Flutter 仿京东商城项目签名验证 增加收货地址、显示收货地址 事件广播
  7. 算法习题---4-3黑白棋(UVa220)
  8. 123457123457#0#-----com.yimeng.wangZheChengYu01--前拼后广--成语头脑王者
  9. Linux hostname介绍
  10. svn如何撤销之前某个版本所做的改变