您的位置:首页 > 其它

Christmas Spruce—codeforces(hello_2018)

2018-01-27 00:03 471 查看
此处传送门

Christmas Spruce

time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard output

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:



打cf从来没想过涨分!只求不掉分就是我最大的愿望了,这场hello 2018是当时在学校写的..ac a和b后就溜去睡觉了((つД`)太弱了…),第二天起来一看被hack了(;д;)…掉了一大波分..不想缩话

题意:

英语不好,看图理解..看给的这个树的非叶节点上是不是都有三个或以上的节点,是的话就是一棵云杉什么的(云杉是百度翻译??),是就输出yes,否则no。

想法:

纯模拟,开一个结构体记录双亲是谁与儿子的数量,然后就模拟咯..

以下是被hack后改正确的代码…o(╥﹏╥)o

#include <iostream>
#include <cstring>

using namespace std;

struct tree
{
int parent;
int son;
};
tree T[1005];

int main()
{
int n;
int i;
int v[1005];

memset(v, 0, sizeof(v));

for(i = 0; i < 1005; i++)
{
T[i].parent = 0;
T[i].son = 0;
}
cin >> n;
int x;
for(i = 2; i <= n; i++)
{
cin >> x;
if(!v[x])
{
T[T[x].parent].son--;
v[x] = 1;
}
T[i].parent = x;
T[x].son++;
}
int flag = 0;
for(i = 1; i <= n; i++)
{
if((T[i].son && T[i].son < 3)||(v[i] && T[i].son < 3))
{
flag = 1;
break;
}
}
if(flag)
{
cout << "No" << endl;
}
else
{
cout << "Yes" << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces acm