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.

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.

Output

  Print "Yes" if the tree is a spruce and "No" otherwise.

Examples
input
4
1
1
1
output
Yes
input
7
1
1
1
2
2
2
output
No
input
8
1
1
1
1
3
3
3
output
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:

题意:给你一颗有根树,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的更多相关文章

  1. [树的度数] Christmas Spruce

    Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed ...

  2. 【Hello 2018 B】Christmas Spruce

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 写个dfs看看是不是每个节点都有3个叶子节点就可以了. [代码] #include <bits/stdc++.h> us ...

  3. Hello 2018 A,B,C,D

    A. Modular Exponentiation time limit per test 1 second memory limit per test 256 megabytes input sta ...

  4. Christmas Trees, Promises和Event Emitters

    今天有同事问我下面这段代码是什么意思: var MyClass = function() { events.EventEmitter.call(this); // 这行是什么意思? }; util.i ...

  5. POJ3160 Father Christmas flymouse[强连通分量 缩点 DP]

    Father Christmas flymouse Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 3241   Accep ...

  6. Father Christmas flymouse--POJ3160Tarjan

    Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Description After retirement as c ...

  7. POJ3013 Big Christmas Tree[转换 最短路]

    Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 23387   Accepted: 5 ...

  8. poj 3013 Big Christmas Tree (最短路径Dijsktra) -- 第一次用优先队列写Dijsktra

    http://poj.org/problem?id=3013 Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total S ...

  9. poj 3013 Big Christmas Tree Djistra

    Big Christmas Tree 题意:图中每个节点和边都有权值,图中找出一颗树,树根为1使得 Σ(树中的节点到树根的距离)*(以该节点为子树的所有节点的权值之和) 结果最小: 分析:直接求出每个 ...

随机推荐

  1. import tensorflow 报错: tf.estimator package not installed.

    import tensorflow 报错: tf.estimator package not installed. 解决方案1: 安装 pip install tensorflow-estimator ...

  2. SpringBoot注解大全(转)

    原文链接:[springBoot系列]--springBoot注解大全 一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Co ...

  3. androidstudio全局搜索快捷键Ctrl+Shift+F失效的解决办法

    与输入法设置冲突!!修改了就可以了.用的搜狗输入法,它的此快捷键也为简繁体替换.修改成其他的即可 null

  4. 解决Android Studio出现Failed to open zip file. Gradle's dependency cache may be corrupt的问题

    问题如下图所示: 解决: 修改 gradle-wrapper.properties里的gradle的版本,与之前没有报错的gradle版本一致.就可以了 比如我报这个错的时候 : distributi ...

  5. Android 安全开发之 ZIP 文件目录遍历

    1.ZIP文件目录遍历简介 因为ZIP压缩包文件中允许存在"../"的字符串,攻击者可以利用多个"../"在解压时改变ZIP包中某个文件的存放位置,覆盖掉应用原 ...

  6. [Unity]Unity常见API

    本文主要为了方便查阅 1. MonoBehaviour 生命周期 Awake 对象创建的时候调用,类似构造函数 Start 在Awake之后执行,区别在于,如果组件不可用(在Inspector没有勾选 ...

  7. libGDX开发环境搭建-Android Studio 最新版

    http://blog.csdn.net/renwuqiangg/article/details/53088720 —————————————————————————————————————————— ...

  8. C#访问gsoap的服务

    C++开发一个webservice,然后C#开发客户端,这样就需要C#的客户端访问gsoap的服务端.(大家都知道gsoap是C/C++开发webservice的最佳利器) 为什么不考虑直接用wcf开 ...

  9. 新内容转入github

    所有新内容已经转入 https://github.com/honggzb/Study-General https://github.com/honggzb/Study2016

  10. php异步执行其他程序

    这里的“其他程序”,可能是linux命令,可能是其他的php文件. 网上说法有四种.分别为: 1.通过加载页面的时候通过ajax技术异步请求服务器 2.通过popen()函数 3.通过curl扩展 4 ...