304787: CF913B. Christmas Spruce

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Christmas Spruce

题意翻译

题意 考虑一颗有根树。一颗有根树有一个叫做根的特殊顶点,所有边的方向都是从根来的。如果有一条从v到u的边,那么顶点v是u的父亲,u是v的孩子。如果一个顶点没有孩子并且有一个父亲,这个顶点叫做叶结点。 如果一颗有根树的每个非叶结点至少有三个是叶结点的孩子,这棵树是一棵云杉。 给你一颗有根树,检查它是否是一棵云杉。 输入格式: 第一行包含一个整数 n(3<=n<=1000),树中的顶点数。 接下来的n-1行每行包含一个整数 p_{i}(1<=i<=n-1,1<=p_{i}<=i),表示第i+1个顶点的父亲。 顶点1为根,保证根最少有两个孩子。 输出格式: 如果它是云杉,输出"Yes",否则,输出 "No"。 Translated by Fowany

题目描述

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](https://goo.gl/1dqvzz).

输入输出格式

输入格式


The first line contains one integer $ n $ — the number of vertices in the tree ( $ 3<=n<=1000 $ ). Each of the next $ n-1 $ lines contains one integer $ p_{i} $ ( $ 1<=i<=n-1 $ ) — the index of the parent of the $ i+1 $ -th vertex ( $ 1<=p_{i}<=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.

输入输出样例

输入样例 #1

4
1
1
1

输出样例 #1

Yes

输入样例 #2

7
1
1
1
2
2
2

输出样例 #2

No

输入样例 #3

8
1
1
1
1
3
3
3

输出样例 #3

Yes

说明

The first example: ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF913B/3d87b6a6cda0ba6f4ad05908fb42ae8248c8369b.png) The second example: ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF913B/bd0b03933e2dbb274b2b58b0c7a13d930c39c80b.png) It is not a spruce, because the non-leaf vertex $ 1 $ has only $ 2 $ leaf children. The third example: ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF913B/a9d72240b2a5e338c43541d320aabfb5ee526dff.png)

Input

题意翻译

### 题目描述 给定一棵树,其中的每个非叶子节点的儿子中,必须有不少于三个节点是叶子节点,满足输出 `YES`,否则输出 `NO`。 ### 输入格式 第一行包含一个整数 $n(3\le n\le 1000)$,表示树的节点数。 接下来的 $n-1$ 行每行包含一个整数 $p_i(1\le p_i\le i)$,$p_i$ 表示第 $i+1$ 号节点的父亲。

加入题单

上一题 下一题 算法标签: