Christmas Spruce—codeforces(hello_2018)

此處傳送門

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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章