zoukankan      html  css  js  c++  java
  • [树的度数] Christmas Spruce

    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:

    题意:问一棵树的所有非叶子结点是否至少有三个叶子
    思路:BFS搜索每个非叶子结点的叶子结点的个数,碰到少于3的输出No,否则若都大于等于3个就输出Yes

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<queue>
     5 using namespace std;
     6 typedef long long ll;
     7 const int amn=1e5+5;
     8 int n,ans=0,m[amn],deep[amn],idx[amn],cnt;
     9 vector<int> eg[amn];
    10 queue<int> q;
    11 int bfs(int rt){
    12     while(q.size())q.pop();q.push(rt);
    13     memset(deep,0,sizeof deep);
    14     memset(idx,0,sizeof idx);
    15     deep[rt]=1;
    16     cnt=0;
    17     while(q.size()){
    18         int u=q.front();q.pop();
    19         idx[u]=1;
    20         cnt=0;
    21         for(int i=0;i<eg[u].size();i++){
    22             int v=eg[u][i];
    23             if(idx[v]||v==u)continue;
    24             if(!eg[v].size())cnt++; ///统计叶子结点个数
    25         }
    26         if(cnt>=3){
    27                 for(int i=0;i<eg[u].size();i++){
    28                 int v=eg[u][i];
    29                 if(idx[v]||v==u)continue;
    30                 if(eg[v].size())q.push(v);
    31             }
    32         }
    33         else return 0;
    34     }
    35     return 1;
    36 }
    37 int main(){
    38     scanf("%d",&n);
    39     for(int i=2;i<=n;i++){
    40         scanf("%d",&m[i]);
    41         eg[m[i]].push_back(i);
    42     }
    43     ans=bfs(1);
    44     if(ans)
    45     printf("Yes
    ");
    46     else printf("No
    ");
    47 }
    48 /**
    49 题意:问一棵树的所有非叶子结点是否至少有三个叶子
    50 思路:BFS搜索每个非叶子结点的叶子结点的个数,碰到少于3的输出No,否则若都大于等于3个就输出Yes
    51 **/
  • 相关阅读:
    tcp socket
    Spring MVC 复习笔记01
    Spring 复习笔记01
    mybatis 复习笔记03
    ActiveMQ实现负载均衡+高可用部署方案(转)
    JVM复习笔记
    JAVA NIO复习笔记
    java IO复习笔记
    并发编程复习笔记
    杂记复习笔记
  • 原文地址:https://www.cnblogs.com/Railgun000/p/11827435.html
Copyright © 2011-2022 走看看