F

题目描叙:

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

分析:

本题,我们可以首先用一个vis数组来将每个不是叶子结点的进行标记。然后,在通过循环,如果当前,这个结点是叶子结点,则将它的父结点++,最后在一个循环,判断父结点下的叶子结点的个数是不是大于3。

#include"stdio.h"
#include"string.h"
int main()
{int n,a[1001],vis[1001],i,j,k;while(~scanf("%d",&n)){memset(vis,0,sizeof(vis));for(i=2;i<=n;i++){scanf("%d",&a[i]);vis[a[i]]=1;//将此结点的父结点为1}for(i=n;i>=2;i--){if(vis[i]==0)//因为当前结点为叶子结点vis[a[i]]++;//将当前结点的父结点++运算,保存此结点有几个叶子结点}for(i=1;i<=n;i++){//这里是因为标记为1,所以<4if(vis[i]&&vis[i]<4)//当前结点为父结点,且叶子结点小于4个break;}if(i>n)printf("Yes\n");elseprintf("No\n");}
}