codeforces - 913B Christmas Spruce(树)

Posted shuitiangong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codeforces - 913B Christmas Spruce(树)相关的知识,希望对你有一定的参考价值。

题目链接:https://vjudge.net/problem/CodeForces-913B

题目描述:

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:

技术图片

大概题意就是给你一颗有根数,如果每个中间结点(或者根结点)的叶的数量大于等于3就是云杉,否则不是。

我们可以递归的进行判断:

1.某结点是否有3个以上的叶结点

2.某结点的非叶结点是否有3个以上的叶结点

#include<set>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<stdio.h>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define endl ‘
‘
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define mst(a) memset(a, 0, sizeof(a))
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const double pi = acos(-1.0);
const double eps = 1e-7;
const int INF = 0x3f3f3f3f;
const int _NAN = -0x3f3f3f3f;
const int NIL = -1;
const int maxn = 1e3+10;
struct node {
    int p, l, r;
};
vector<int> trees[maxn];
bool solve(int root) {
    int cnt = 0;
    for (int i = 0; i<trees[root].size(); ++i)
        if (trees[trees[root][i]].empty())
            ++cnt;
    if (cnt < 3)
        return false;
    else {
        for (int i = 0; i<trees[root].size(); ++i)
            if (!trees[trees[root][i]].empty())
                //solve(trees[root][i]); 错误1:更深一步的递归不影响之前的结果
                //return solve(trees[root][i]); 错误2:如果第一次递归是true即使后面是false也不影响
                if (!solve(trees[root][i])) 
                    return false;
    }
    return true;
}
int main(void) {
    int n;
    while(~scanf("%d", &n)) {
        for (int i = 2, node; i<=n; ++i) {
            scanf("%d", &node);
            trees[node].push_back(i);
        }
        printf(solve(1) ? "Yes
" : "No
");
        for (int i = 0; i<maxn; ++i)
            trees[i].clear();
    }
    return 0;
}

 

以上是关于codeforces - 913B Christmas Spruce(树)的主要内容,如果未能解决你的问题,请参考以下文章

现在的位置就是对的啊,意思就是当鼠标放上去的时候,停止滚动,鼠标离开,继续滚动

codeforces上怎么看测试数据

如何看codeforces做了多少题

codeforces上怎么看测试数据

codeforces比赛后怎么看题解和答案

codeforces是啥?