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:

题意:问一棵树的所有非叶子结点是否至少有三个叶子
思路: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
**/

最新文章

  1. Android开发自学笔记(Android Studio)&mdash;4.2TextView及其子类
  2. @Autowired的使用
  3. Contributing to the C++ Core Guidelines
  4. Android 常用操作
  5. linux下安装oracle
  6. ubuntu共享文件配置
  7. tps (事务处理系统)
  8. Assert断言测试
  9. 深入分析:Fragment与Activity交互的几种方式(三,使用接口)
  10. 很酷的CSS3仿Facebook登录表单
  11. 深度学习:Keras入门(二)之卷积神经网络(CNN)
  12. Linux开发-makefile
  13. 初涉wheel 组
  14. sklearn中的SVM
  15. RichTextbox下Hyperlink的Click无效
  16. Blender简单动画:一个小球从一座山上滚下.
  17. 教你如何自学UI设计
  18. asp.net gridview 如何实现行点击事件
  19. pyqt5和qtdesign的使用
  20. tomcat启动(三)Catalina简要分析

热门文章

  1. appium ios真机自动化环境搭建&运行(送源码)
  2. SQL注入攻击浅谈
  3. USB小白学习之路(1) Cypress固件架构解析
  4. AF(操作者框架)系列(3)-创建第一个Actor的程序
  5. 基于arduino的红外传感系统
  6. 统计 Django 项目的测试覆盖率
  7. GitHub 热点速览 vol.10:疫情下的 GitHub
  8. Python 解密JWT验证苹果登录
  9. ASP.NET Core身份认证服务框架IdentityServer4 介绍
  10. 微博立场检测 60分Baseline