题目描述
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 106.

输出
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

思路
找树的根节点,从根节点向叶节点推,确认每个节点的子节点是否为k个,叶节点的深度是否相同,注意n=1时答案为1,初始化图时不要用memset。

代码实现

#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>using namespace std;
const int N=1000005;
typedef long long ll;
struct node
{int to,index;
}g[N<<1];
int n;
int head[N<<1],cnt,deg[N];
int dep[N];inline void add(int u,int v)
{g[++cnt].to=v;g[cnt].index=head[u];head[u]=cnt;g[++cnt].to=u;g[cnt].index=head[v];head[v]=cnt;
}void dfs(int u,int nxt,int d)
{dep[u]=d;for(int i=head[u];i;i=g[i].index){int v=g[i].to;if(v==nxt) continue;dfs(v,u,d+1);}
}
int main()
{int T;scanf("%d",&T);while(T--){scanf("%d",&n);for(int i=0;i<=n;i++) head[i]=deg[i]=0;cnt=0;for(int i=1;i<n;i++){int u,v;scanf("%d%d",&u,&v);add(u,v);deg[u]++;deg[v]++;}if(n<=3){puts("1");continue;}int maxl=0;bool flag=false;for(int i=1;i<=n;i++){if(deg[i]==n-1){printf("%d\n",deg[i]);flag=true;break;}maxl=max(maxl,deg[i]);}if(flag) continue;int k=maxl-1,rt;for(int i=1;i<=n;i++){if(deg[i]==k){flag=true;rt=i;break;}}if(!flag){puts("-1");continue;}flag=false;dfs(rt,-1,0);int mosd=-1;for(int i=1;i<=n;i++){if(i!=rt){if(deg[i]==1){if(mosd==-1) mosd=dep[i];else if(mosd!=dep[i]){flag=true;break;}}else{if(deg[i]!=k+1){flag=true;break;}}}else{if(deg[i]!=k){flag=true;break;}}}if(flag) puts("-1");else printf("%d\n",k);}return 0;
}

Perfect Tree(图论)相关推荐

  1. 中石油训练赛 - Perfect Tree(dfs)

    题目描述 Given a positive integer k, we define a rooted tree to be k-perfect, if and only if it meets bo ...

  2. 2020牛客暑期多校训练营Decrement on the Tree(图论,set)

    Decrement on the Tree 题目描述 样例 input: 5 3 1 2 3 1 3 4 2 4 5 3 5 6 1 10 2 10 3 10 output: 8 12 10 10 题 ...

  3. Gardener and Tree 图论,树,队列

    题意 : 给一无根树,每次操作将所有叶子结点去除,问k次操作后还剩多少结点 思路 : 输入时存每个点的入度和出度,然后把度 小于等于 1的放入队列并记录为第一层,然后从队头开始弹出元素,枚举当前点的邻 ...

  4. NOIP 好题推荐(DP+搜索+图论)POJ ZOJ

    NOIP好题推荐(DP+搜索+图论)POJ ZOJ 1370 Gossiping (数论->模线性方程有无解的判断)+(图论->DFS)  1090 Chain ->格雷码和二进制码 ...

  5. 1159 Structure of a Binary Tree(中序+后序建树|丑陋输入的处理|full tree的定义)

    step1:利用所给的中序和后序模拟实现链表树的构建(PAT分治建树的其他题:分治建堆) step2:恶心输入的处理,注意使用getline,vector,stoi step3:各种询问的处理,注意f ...

  6. POJ的题目分类(两个版本)

    版本一: 简单题 1000A+B Problem 1001Exponentiation 1003 Hangover 1004 Financial Management 1005 I Think I N ...

  7. POJ前面的题目算法思路【转】

    1000 A+B Problem 送分题 49% 2005-5-7 1001 Exponentiation 高精度 85% 2005-5-7 1002 487-3279 n/a 90% 2005-5- ...

  8. [转载]常用数学专业名词的英语

    目录 前言 数学分支的英文: 命题的英文: 数学中常见数词的英语: 点的英文: 线的英文: 面的英文: 角的英文: 距离的英文: 多边形的英文: 三角形的英文: 四边形的英文: 圆的英文: 多面体的英 ...

  9. POJ 超详细分类

    POJ 各题算法 1000    A+B Problem            送分题     49%    2005-5-7 1001    Exponentiation         高精度   ...

最新文章

  1. 微信网页JSDK接口-wx.chooseImage问题
  2. PortICASetDefaults.exe /o命令返回为空
  3. 把linux插足到域
  4. node.js 创建服务器_Node.js HTTP软件包–创建HTTP服务器
  5. 贪心算法之加勒比海盗问题
  6. python中re怎么念_Python,Re模块的学习
  7. 各自然带代表植被_十种常见自然带所对应的植被 十种常见自然带所对应的气候类型...
  8. Apache安装教程
  9. EZchip花1.3亿美元买Tilera然后以8亿美元把自己与Tilera一起卖掉
  10. 【使用SqliteSpy访问Sqlite3数据库】
  11. 基于STM32F407的万能红外遥控器
  12. iOS-Core-Animation-Advanced-Techniques(一)
  13. Grid Control一些术语GC、OMS、OMR、OMA的概念
  14. 安卓电子书格式_如何将电子书导入kindle App?
  15. 进程的三种状态及相互之间的转换
  16. unity粒子系统stop()和clear()
  17. c语言 计算分段函数
  18. PHP实现微信公众号主动推送消息
  19. 浏览器真的能“永不假死”?——六款主流浏览器防假死功能测试
  20. 闲来无事系列-雨课堂抓包发包玩法(娱乐向)

热门文章

  1. 曙光服务器主板显示Fd,解析曙光八路服务器A950r-F、I950r-G
  2. 关于APP广告位的设计与优化(上)
  3. linux系统安装python包
  4. BigDecimal如何保留小数位
  5. 明翰的大数据笔记V0.2(持续更新)
  6. C++对象构建与使用,error: taking address of temporary [-fpermissive]
  7. 【Java8 环境安装】Java1.8JDK环境安装jdk-8u361-windows-x64
  8. IDEA中的路径问题
  9. Notability for Mac(笔记软件)
  10. teradata安装