Christmas Spruce
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.
Input
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.
Print "Yes" if the tree is a spruce and "No" otherwise.
4
1
1
1
Yes
7
1
1
1
2
2
2
No
8
1
1
1
1
3
3
3
Yes
The second example:

It is not a spruce, because the non-leaf vertex 1 has only 2 leaf children.
The third example:

题意:给你一颗有根树,n个结点第一个结点为根结点。输入n-1行,代表从第二个结点开始的当前结点的父结点,例如样例一:第一个1代表2号结点父结点是1,
同理,3,4号父结点也是1。于是就连接成了样例一那种树。让我们判断每个非叶子结点的叶子个数是否 >=3 ,满足就输入Yes,否则就输出No.
分析:模拟,开个vis数组标记每个非叶子节点为1,叶子节点为0,从n到1扫一次,找叶子节点,就代表父结点有个满足条件的孩子。最后扫一次,扫每个非叶子节点,判断他的孩子是否>=3,就满足条件
输出yes.
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = ;
int a[maxn],vis[maxn]; int main(){
int n;
while(cin>>n){
memset(vis,,sizeof(vis));
for( int i=; i<=n; i++ ){
cin>>a[i];
vis[a[i]]=;//非叶子节点标记为1
}
for( int i=n; i>=; i-- ){
if(!vis[i]) vis[a[i]]++;
}
int flag=;
for( int i=; i<=n; i++ ){
if(vis[i]&&vis[i]<){
flag=;
break;
}
}
if(flag) cout<<"Yes"<<endl;
else{
cout<<"No"<<endl;
} }
return ;
}
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 ...
 - Hello 2018 A,B,C,D
		
A. Modular Exponentiation time limit per test 1 second memory limit per test 256 megabytes input sta ...
 - Christmas Trees, Promises和Event Emitters
		
今天有同事问我下面这段代码是什么意思: var MyClass = function() { events.EventEmitter.call(this); // 这行是什么意思? }; util.i ...
 - POJ3160 Father Christmas flymouse[强连通分量 缩点 DP]
		
Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 3241 Accep ...
 - Father Christmas flymouse--POJ3160Tarjan
		
Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Description After retirement as c ...
 - POJ3013 Big Christmas Tree[转换 最短路]
		
Big Christmas Tree Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 23387 Accepted: 5 ...
 - poj 3013 Big Christmas Tree (最短路径Dijsktra) -- 第一次用优先队列写Dijsktra
		
http://poj.org/problem?id=3013 Big Christmas Tree Time Limit: 3000MS Memory Limit: 131072K Total S ...
 - poj 3013 Big Christmas Tree  Djistra
		
Big Christmas Tree 题意:图中每个节点和边都有权值,图中找出一颗树,树根为1使得 Σ(树中的节点到树根的距离)*(以该节点为子树的所有节点的权值之和) 结果最小: 分析:直接求出每个 ...
 
随机推荐
- 自定义spring参数注解 - 打破@RequestBody单体限制
			
本文主要描述怎样自定义类似@RequestBody这样的参数注解来打破@RequestBody的单体限制. 目录1 @RequestBody的单体限制2 自定义spring的参数注解3 编写sprin ...
 - FFM及DeepFFM模型在推荐系统的探索及实践
			
12月20日至23日,全球人工智能与机器学习技术大会 AiCon 2018 在北京国际会议中心盛大举行,新浪微博AI Lab 的资深算法专家 张俊林@张俊林say 主持了大会的 搜索推荐与算法专题,并 ...
 - python接口自动化测试(八)-unittest-生成测试报告
			
用例的管理问题解决了后,接下来要考虑的就是报告我问题了,这里生成测试报告主要用到 HTMLTestRunner.py 这个模块,下面简单介绍一下如何使用: 一.下载HTMLTestRunner下载: ...
 - GopherChina 2018
			
https://github.com/gopherchina/conference/tree/master/2018
 - SQL Server 性能优化实战系列(二)
			
SQL Server datetime数据类型设计.优化误区 一.场景 在SQL Server 2005中,有一个表TestDatetime,其中Dates这个字段的数据类型是datetime,如果你 ...
 - mac 下 通过 brew 安装 MariaDB
			
其实在两年多前,我就推荐大家使用MariaDB了,其实真的很好用,性能高,也可以完全替代mysql 主要是这oracle实在是太**了,java都收费了,mysql迟早的事... 安装MariaDB之 ...
 - 如何用jQuery获取选中行固定列的数据
			
[本文出自天外归云的博客园] 问题:把选中行的ID统计出来,组成一个数组传给后台(选中行的特点:class为danger) 办法如下: // 多选后点击下线按钮 $("#offline&qu ...
 - SparkThriftServer  源码分析
			
目录 版本 起点 客户端--Beeline 服务端 Hive-jdbc TCLIService.Iface客户端请求 流程 SparkThrift 主函数HiveThriftServer2 Thrif ...
 - Linux之文件系统各种符号说明
			
/ 根目录 唯一必须挂载的目录.不要有任何的犹豫,选一个分区,挂载它!(在绝大多数情况下,有10G的容量应该是够用了.当然了,很多东西都是多多益善的) /boot 它包含了操作系统的内核和在启动系统过 ...
 - vs2017离线安装且安装包不占用C盘空间
			
[参考]vs2017离线安装且安装包不占用C盘空间 第一步:下载离线安装包 https://www.visualstudio.com/zh-hans/downloads/ 在官方地址下载vs_prof ...