[树的度数] Christmas Spruce
Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed from the root. Vertex u is called a child of vertex v and vertex v is called a parent of vertex u if there exists a directed edge from v to u. A vertex is called a leaf if it doesn't have children and has a parent.
Let's call a rooted tree a spruce if its every non-leaf vertex has at least 3 leaf children. You are given a rooted tree, check whether it's a spruce.
The definition of a rooted tree can be found here.
The first line contains one integer n — the number of vertices in the tree (3 ≤ n ≤ 1 000). Each of the next n - 1 lines contains one integer pi (1 ≤ i ≤ n - 1) — the index of the parent of the i + 1-th vertex (1 ≤ pi ≤ i).
Vertex 1 is the root. It's guaranteed that the root has at least 2 children.
Output
Print "Yes" if the tree is a spruce and "No" otherwise.
Examples
4
1
1
1
Yes
7
1
1
1
2
2
2
No
8
1
1
1
1
3
3
3
Yes
Note
The first example:
The second example:
It is not a spruce, because the non-leaf vertex 1 has only 2 leaf children.
The third example:
题意:问一棵树的所有非叶子结点是否至少有三个叶子
思路:BFS搜索每个非叶子结点的叶子结点的个数,碰到少于3的输出No,否则若都大于等于3个就输出Yes
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
typedef long long ll;
const int amn=1e5+;
int n,ans=,m[amn],deep[amn],idx[amn],cnt;
vector<int> eg[amn];
queue<int> q;
int bfs(int rt){
while(q.size())q.pop();q.push(rt);
memset(deep,,sizeof deep);
memset(idx,,sizeof idx);
deep[rt]=;
cnt=;
while(q.size()){
int u=q.front();q.pop();
idx[u]=;
cnt=;
for(int i=;i<eg[u].size();i++){
int v=eg[u][i];
if(idx[v]||v==u)continue;
if(!eg[v].size())cnt++; ///统计叶子结点个数
}
if(cnt>=){
for(int i=;i<eg[u].size();i++){
int v=eg[u][i];
if(idx[v]||v==u)continue;
if(eg[v].size())q.push(v);
}
}
else return ;
}
return ;
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&m[i]);
eg[m[i]].push_back(i);
}
ans=bfs();
if(ans)
printf("Yes\n");
else printf("No\n");
}
/**
题意:问一棵树的所有非叶子结点是否至少有三个叶子
思路:BFS搜索每个非叶子结点的叶子结点的个数,碰到少于3的输出No,否则若都大于等于3个就输出Yes
**/
[树的度数] Christmas Spruce的更多相关文章
- Christmas Spruce
Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed ...
- 【Hello 2018 B】Christmas Spruce
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 写个dfs看看是不是每个节点都有3个叶子节点就可以了. [代码] #include <bits/stdc++.h> us ...
- 笔试算法题(46):简介 - 二叉堆 & 二项树 & 二项堆 & 斐波那契堆
二叉堆(Binary Heap) 二叉堆是完全二叉树(或者近似完全二叉树):其满足堆的特性:父节点的值>=(<=)任何一个子节点的键值,并且每个左子树或者右子树都是一 个二叉堆(最小堆或者 ...
- <算法导论>高级数据结构--以我的角度看B树(Balanced-Tree)的建增删查
题外话:在博客园看了几篇关于B树的博文确实很有帮助,但是也看到有一些Funny的博文- -比如拿二叉树堂(BinaryTree)而皇之写上B树的帽子. 好了题归正传,B树(Balanced-Tree) ...
- 【BZOJ4474】isomorphism(树的同构,哈希)
题意:一个无向树的度数为 2的结点称为假结点,其它结点称为真结点.一个无向树的简化树其结点由原树的全体真结点组成,两个真结点之间有边当且仅当它们在原树中有边,或者在原树中有一条联结这两个结点的路,其中 ...
- 【51NOD1806】wangyurzee的树(Prufer编码,容斥原理,组合计数)
题意:有n个点和m条限制,每条限制限制了一个点的度数不能为某个数. 求合法的树的个数模10^9+7 n<=10^6 m<=17 思路:WYZ作业 首先m<=17显然是2^m容斥 枚举 ...
- 【hihocoder 1511】树的方差
[题目链接]:http://hihocoder.com/problemset/problem/1511 [题意] [题解] 有个方差的公式 V(X)=E(X2)−E(X)2 这里E(X)指的是X的期望 ...
- POJ 3177--Redundant Paths【无向图添加最少的边成为边双连通图 && tarjan求ebc && 缩点构造缩点树】
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10798 Accepted: 4626 ...
- Python入门篇-数据结构树(tree)篇
Python入门篇-数据结构树(tree)篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.树概述 1>.树的概念 非线性结构,每个元素可以有多个前躯和后继 树是n(n& ...
随机推荐
- 自研接口测试平台(Django2+Bootstrap3+Unittest)
自研接口测试平台(Django2+Bootstrap3+Unittest) HttpRuleTest是一款面向 HTTP 协议的通用测试框架,只需web端维护接口测试数据,即可实现自动化测试.监控.数 ...
- python设置检查点简单实现
说检查点,其实就是对过去历史的记录,可以认为是log.不过这里进行了简化.举例来说,我现在又一段文本.文本里放有一堆堆的链接地址.我现在的任务是下载那些地址中的内容.另外因为网络的问题或者网站的问题, ...
- 阿里云ESC学生服务器搭建springboot项目生产环境(Mysql+JDK)不需要上传安装包
嗯,之前服务器被挖矿的病毒弄的登录不进去了,所以联系了阿里云客服,提交工单,最后建议重置,所以我就重置了, 嗯,学习经验,docker如果懂的不是太多,不要随便云部署,都给别人挖矿了. Mysql ...
- 关于C++类中的三兄弟(pretect、private、public)
1.public修饰的成员变量 在程序的任何地方都可以被访问,就是公共变量的意思,不需要通过成员函数就可以由类的实例直接访问 2.private修饰的成员变量 只有类内可直接访问,私有的,类的实例要通 ...
- Python开发(一):Python介绍与基础知识
Python开发(一):Python介绍与基础知识 本次内容 一:Python介绍: 二:Python是一门什么语言 三:Python:安装 四:第一个程序 “Hello world” 五:Pytho ...
- jstack的使用
一.概述 有些时候我们需要查看下jvm中的线程执行情况,比如,发现服务器的CPU的负载突然增高了.出现了死锁.死循环等,我们该如何分析呢? 由于程序是正常运行的,没有任何的输出,从日志方面也看不出什么 ...
- node--静态文件托管,路由,模板引擎
1.路由 路由是由一个URI和一个特定的HTTP方法(GET/POST)组成的 涉及到应用如何响应客户端对某个网站节点的访问 2.ejs 3.get/post 1)get获取数据 通过Url类中的qu ...
- 利用virtualenvwrapper创建虚拟环境出现错误“/usr/bin/python: No module named virtualenvwrapper”
Linux:CentOS7 python: 系统默认python版本2.7,利用python启动 自己安装python版本3.8,利用python3启动 问题描述: 在上述环境中利用virtualen ...
- 关于SSH与SSM的组成及其区别
前言 当下SpringBoot盛行,咱再聊聊SpringBoot盛行之前的框架组合,当做复习巩固哈. 在聊之前,得先说说MVC,MVC全名是Model View Controller,是模型(mode ...
- js中的this和arguments.callee
this和 arguments.callee this 全局作用域下,this指向Window 其他情况下,谁调用this就指向谁 console.log(this) //对调用就指向谁系列 func ...