题目描述

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 */

【满k叉树】Perfect Tree的更多相关文章

  1. n层满k叉树总共有多少个节点

    2叉树 1 3 7 对应公式为(2^n-1)/1 3叉树 1 4 13 对应公式为(3^n-1)/2 4叉树 1 5 21对应公式为(4^n-1)/3 ... n层k叉树,总共有(k^n-1)/k-1 ...

  2. HDU 6121 Build a tree(完全K叉树)

    http://acm.hdu.edu.cn/showproblem.php?pid=6121 题意:给你一颗完全K叉树,求出每棵子树的节点个数的异或和. 思路: 首先需要了解一些关于完全K叉树或满K叉 ...

  3. 2017多校第7场 HDU 6121 Build a tree K叉树,思维

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6121 题意:一个n个点的完全k叉树,求每个节点的size的异或和. 解法:容易发现,考虑根的所有孩子, ...

  4. HDU 6121 Build a tree(k叉树的子树大小相异)

    http://acm.hdu.edu.cn/showproblem.php?pid=6121 题目大意: 给你一颗 n 个节点的完全 k 叉树,问你这棵树中所有子树结点个数的总异或值. 分析: 我们很 ...

  5. Comet OJ 夏季欢乐赛 完全k叉树

    完全k叉树 https://cometoj.com/contest/59/problem/A?problem_id=2712 题目描述 欢迎报考JWJU!这里有丰富的社团活动,比如为梦想奋斗的ACM集 ...

  6. 排序算法 以及HKU的一些数据结构 相关题目 以及 K叉树,二叉树 排列

    冒泡排序.选择排序.快速排序.插入排序.希尔排序.归并排序.基数排序以及堆排序,桶排序 https://www.cnblogs.com/Glory-D/p/7884525.html https://b ...

  7. 【最优K叉树】hdu 5884 Sort

    http://acm.hdu.edu.cn/showproblem.php?pid=5884 参考:https://www.cnblogs.com/jhz033/p/5879452.html [题意] ...

  8. 回溯法、子集树、排列树、满m叉树

    显示图: 明确给出了图中的各顶点及边 隐式图: 仅给出初始节点.目标节点及产生子节点的条件(一般有问题提议隐含给出)的情况下,构造一个图. 回溯法: 从初始状态出发,在隐式图中以深度优先的方式搜索问题 ...

  9. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

随机推荐

  1. 什么是IO流

    先看一段百度上的解释: 当然:如果你看不懂,那么你只需要记住下面3句话. 1. (1).我们知道,每个人家里喝的水都是从自来水厂来的,自来水厂的水又是从水源地来的, (2).水是通过水管来的. (3) ...

  2. 如何在虚拟机中安装kali linux

    整理笔记,把以前印象笔记中记录的一些东西翻出来,想想发个随笔吧. 第一步在官网下载kali linux的镜像. 网址:https://www.kali.org/downloads/ (我的电脑是64位 ...

  3. Flutter移动电商实战 --(45)详细页_说明区域UI编写

    pages/details_page/details_expain.dart 详情页面引用组件 效果展示: 最终代码: import 'package:flutter/material.dart'; ...

  4. Vue引入远程JS文件

    问题 最近在使用 Vue 做东西,用到钉钉扫描登录的功能,这里需要引入远程的 js 文件,因为 Vue 的方式跟之前的不太一样,又不想把文件下载到本地应用,找了一下解决的方法,貌似都需要引入第三方的库 ...

  5. linux内核中的regmap是如何初始化的?

    1. 内核版本 5.2.0 2. 请看devm_regmap_init_i2c (include/linux/regmap.h) /** * devm_regmap_init_i2c() - Init ...

  6. js文件中如何使用 获取EL表达式的值

    转: js文件中如何使用 获取EL表达式的值 原先做法是在jsp页面引入头文件 <%@ page language="java" pageEncoding="UTF ...

  7. 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_13-SpringSecurityOauth2研究-JWT研究-生成JWT令牌&验证JWT令牌

    生成jwt需要用私钥来签名.在Auth认证服务下创建测试类 创建密钥工厂,构造函数需要的参数 获取私钥 有了私钥就可以生成JWT令牌 使用jwtHelper是spring security里面的类 e ...

  8. (十二)class文件结构:魔数和版本

    一.java体系结构 二.class格式文件概述 class文件是一种8位字节的二进制流文件, 各个数据项按顺序紧密的从前向后排列, 相邻的项之间没有间隙, 这样可以使得class文件非常紧凑, 体积 ...

  9. Struts 2 --ONGL介绍

    先了解一下OGNL的概念 OGNL的全名称Object Graph Navigation Language.全称为对象图导航语言,是一种表达式语言.使用这种表达式语言,你可以通过某种表达式语法,存取J ...

  10. Direct2D 学习笔记 前言

    Direct2D模板程序网址:https://docs.microsoft.com/zh-cn/windows/win32/direct2d/direct2d-quickstart DirectX S ...