[树的度数] 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.
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
4
1
1
1
Yes
7
1
1
1
2
2
2
No
8
1
1
1
1
3
3
3
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
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
typedef long long ll;
const int amn=1e5+;
int n,ans=,m[amn],deep[amn],idx[amn],cnt;
vector<int> eg[amn];
queue<int> q;
int bfs(int rt){
while(q.size())q.pop();q.push(rt);
memset(deep,,sizeof deep);
memset(idx,,sizeof idx);
deep[rt]=;
cnt=;
while(q.size()){
int u=q.front();q.pop();
idx[u]=;
cnt=;
for(int i=;i<eg[u].size();i++){
int v=eg[u][i];
if(idx[v]||v==u)continue;
if(!eg[v].size())cnt++; ///统计叶子结点个数
}
if(cnt>=){
for(int i=;i<eg[u].size();i++){
int v=eg[u][i];
if(idx[v]||v==u)continue;
if(eg[v].size())q.push(v);
}
}
else return ;
}
return ;
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&m[i]);
eg[m[i]].push_back(i);
}
ans=bfs();
if(ans)
printf("Yes\n");
else printf("No\n");
}
/**
题意:问一棵树的所有非叶子结点是否至少有三个叶子
思路:BFS搜索每个非叶子结点的叶子结点的个数,碰到少于3的输出No,否则若都大于等于3个就输出Yes
**/
[树的度数] Christmas Spruce的更多相关文章
- Christmas Spruce
Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed ...
- 【Hello 2018 B】Christmas Spruce
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 写个dfs看看是不是每个节点都有3个叶子节点就可以了. [代码] #include <bits/stdc++.h> us ...
- 笔试算法题(46):简介 - 二叉堆 & 二项树 & 二项堆 & 斐波那契堆
二叉堆(Binary Heap) 二叉堆是完全二叉树(或者近似完全二叉树):其满足堆的特性:父节点的值>=(<=)任何一个子节点的键值,并且每个左子树或者右子树都是一 个二叉堆(最小堆或者 ...
- <算法导论>高级数据结构--以我的角度看B树(Balanced-Tree)的建增删查
题外话:在博客园看了几篇关于B树的博文确实很有帮助,但是也看到有一些Funny的博文- -比如拿二叉树堂(BinaryTree)而皇之写上B树的帽子. 好了题归正传,B树(Balanced-Tree) ...
- 【BZOJ4474】isomorphism(树的同构,哈希)
题意:一个无向树的度数为 2的结点称为假结点,其它结点称为真结点.一个无向树的简化树其结点由原树的全体真结点组成,两个真结点之间有边当且仅当它们在原树中有边,或者在原树中有一条联结这两个结点的路,其中 ...
- 【51NOD1806】wangyurzee的树(Prufer编码,容斥原理,组合计数)
题意:有n个点和m条限制,每条限制限制了一个点的度数不能为某个数. 求合法的树的个数模10^9+7 n<=10^6 m<=17 思路:WYZ作业 首先m<=17显然是2^m容斥 枚举 ...
- 【hihocoder 1511】树的方差
[题目链接]:http://hihocoder.com/problemset/problem/1511 [题意] [题解] 有个方差的公式 V(X)=E(X2)−E(X)2 这里E(X)指的是X的期望 ...
- POJ 3177--Redundant Paths【无向图添加最少的边成为边双连通图 && tarjan求ebc && 缩点构造缩点树】
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10798 Accepted: 4626 ...
- Python入门篇-数据结构树(tree)篇
Python入门篇-数据结构树(tree)篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.树概述 1>.树的概念 非线性结构,每个元素可以有多个前躯和后继 树是n(n& ...
随机推荐
- Ubuntu18.04制作本地源
Ubuntu 18.04 制作本地源 1. 在可联网的Ubuntu18.04上制作源 创建目录 mkdir /opt/debs 最好在目标电脑上创建相同的目录,以免 apt-get install 时 ...
- 高阶函数---swift中的泛型介绍(一步步实现Map函数)
说明 本文内容均出自函数式 Swift一书, 此处整理仅仅是为了自己日后方便查看, 需要深入研究的话, 可以点进去购买, 支持原作者 本书由 王巍–新浪微博大神翻译 OneV's Den 喵神博客 接 ...
- Koa 学习
中间件引擎 1234567891011121314151617181920212223242526272829303132333435363738 const Koa = require('koa') ...
- 【快速上手】Git的使用教程
创建Git仓库 git init 查看当前仓库情况 git status 添加修改 git add (file) or git add . 查看未提交的修改 git diff 撤销提交操作 git r ...
- C++扬帆远航——15(项目二,太乐了)
/* * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:tailezhanshi.cpp * 作者:常轩 * 微信公众号 ...
- 7-5 jmu-python-分段函数1 (10 分)
本题目要求计算下列分段函数f(x)的值(x为从键盘输入的一个任意实数): 输入格式: 直接输入一个实数给 x,没有其他任何附加字符. 输出格式: 在一行中按“f(x)=result”的格式输出,其中x ...
- VueX状态管理器 的应用
VueX状态管理器 cnpm i vuex axios -S 1 创建Vuex 仓库 import Vue from 'vue' import Vuex from 'vuex' vue.use(Vue ...
- JZOJ 1775. 合并果子2 (Standard IO)
1775. 合并果子2 (Standard IO) Time Limits: 1000 ms Memory Limits: 65536 KB Description 在一个果园里,多多已经将所有的果子 ...
- 使用ZXingObjC扫描二维码横竖屏对应
/** 根据屏幕的方向设置扫描的方向 * @author maguang * @param parameter * @return result */ - (void)showaCapture { C ...
- 关于.NET中的控制反转及AutoFac的简单说明
目录 1.控制反转 1.1 什么是依赖? 1.2 什么是控制反转? 1.3 什么是依赖注入? 1.4 简单总结 2.控制反转容器 2.1 IOC容器说明 2.2 使用AutoFac的简介示例 3 使用 ...