E. Bear and Drawing
1 second
256 megabytes
standard input
standard output
Limak is a little bear who learns to draw. People usually start with houses, fences and flowers but why would bears do it? Limak lives in the forest and he decides to draw a tree.
Recall that tree is a connected graph consisting of n vertices and n - 1 edges.
Limak chose a tree with n vertices. He has infinite strip of paper with two parallel rows of dots. Little bear wants to assign vertices of a tree to some n distinct dots on a paper so that edges would intersect only at their endpoints — drawn tree must be planar. Below you can see one of correct drawings for the first sample test.

Is it possible for Limak to draw chosen tree?
The first line contains single integer n (1 ≤ n ≤ 105).
Next n - 1 lines contain description of a tree. i-th of them contains two space-separated integers ai and bi(1 ≤ ai, bi ≤ n, ai ≠ bi) denoting an edge between vertices ai and bi. It's guaranteed that given description forms a tree.
Print "Yes" (without the quotes) if Limak can draw chosen tree. Otherwise, print "No" (without the quotes).
8
1 2
1 3
1 6
6 4
6 7
6 5
7 8
Yes
13
1 2
1 3
1 4
2 5
2 6
2 7
3 8
3 9
3 10
4 11
4 12
4 13
No 我们想如果一个点 连接着 大于等于两颗树是多叉树的话,那么我们就可以认为这个是无解的(除非其中一颗树是直接连接在该结点上的)。
#include<cstdio>
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
const int maxn = + ;
vector<int> G[maxn];
int leg[maxn];
bool del[maxn];
void dfs(int cur, int per=-)
{
if(G[cur].size()<=)
{
del[cur]=true;
int siz=G[cur].size();
for(int i=; i<siz; i++)
{
int b=G[cur][i];
if(b == per) continue;
dfs(b,cur);
}
} }
int main() {
int n;
while(scanf("%d",&n)==)
{
memset(del,false,sizeof(del));
memset(leg,,sizeof(leg));
for(int i=; i<=n; i++)G[i].clear();
for(int i=; i<n; i++)
{
int a,b;
scanf("%d%d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
}
for(int i=; i<=n; i++)
if(G[i].size() == ){
dfs(i);
}
for(int a=; a<=n; a++)
{
int siz=G[a].size();
for(int j = ; j<siz; j++)
{
int b=G[a][j];
if(del[b])
{
leg[a]=min(leg[a]+,);
}
}
}
bool falg=true;
for(int a=; a<=n; a++)
if(del[a]==false){
int cnt=;
for(int j=; j<G[a].size(); j++)
{
int b=G[a][j];
if(del[b]==false&&G[b].size()-leg[b]>) cnt++;
}
if(cnt>){
falg=false; break;
}
}
if(falg)puts("YES");
else puts("NO");
}
return ;
}
E. Bear and Drawing的更多相关文章
- codeforces 573C Bear and Drawing
Limak is a little bear who learns to draw. People usually start with houses, fences and flowers but ...
- CF573C Bear and Drawing 构造+树论
正解:构造 解题报告: 传送门! 这题首先可以画下图找下规律,,,然后通过找规律可以发现,最终的方案一定是一条主干+一些枝条,而且这些枝条的分杈一定小于等于2 明确一下主干的定义,最左边的节点和最右边 ...
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) C. Bear and Drawing
题目链接:http://codeforces.com/contest/573/problem/C题目大意:在两行无限长的点列上面画n个点以及n-1条边使得构成一棵树,并且要求边都在同一平面上且除了节点 ...
- Codeforces Round #318(Div 1) 573A, 573B,573C
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 这场的前两题完全是手速题...A题写了7分钟,交的时候已经500+了,好在B题出的 ...
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)
以后每做完一场CF,解题报告都写在一起吧 暴力||二分 A - Bear and Elections 题意:有n个候选人,第一个候选人可以贿赂其他人拿到他们的票,问最少要贿赂多少张票第一个人才能赢 ...
- D. Bear and Two Paths(贪心构造)
D. Bear and Two Paths time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #356 (Div. 1) C. Bear and Square Grid
C. Bear and Square Grid time limit per test 3 seconds memory limit per test 256 megabytes input stan ...
- VK Cup 2016 D. Bear and Two Paths 模拟
D. Bear and Two Paths time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Codefoces 791D. Bear and Tree Jumps 树形DP
D. Bear and Tree Jumps A tree is an undirected connected graph without cycles. The distance betwee ...
随机推荐
- PSU/OPATCH/OJVM下载页面及安装方式(最实用版)
中文版:数据库 PSU,SPU(CPU),Bundle Patches 和 Patchsets 补丁号码快速参考 (文档 ID 1922396.1) Download Reference for Or ...
- int(1)和int(11)是否有区别?
MySQL类型关键字后面的括号内指定整数值的显示宽度(例如,INT(11)).该可选显示宽度规定用于显示宽度小于指定的列宽度的值时从左侧填满宽度.显示宽度并不限制可以在列内保存的值的范围,也不限制超过 ...
- 2018天猫双11各类目品牌成交额top10排行榜
2018天猫双11总成交额213,550,497,011元,你知道各类目品牌成交额排行吗?一起来看看吧,赶紧收藏,以后就知道要怎么买了! 相关阅读: 2018天猫双11各类目品牌成交额top10排行榜 ...
- Ubuntu如何修改默认python版本为python3
修改默认python版本 Ubuntu系统内置python2.7版本,默认运行python就是运行python2.7.现在很多人都是用python3作为开发语言,我们需要修改python的默认版本为p ...
- what's the python之if判断、while循环以及for循环
Python缩进原则 顶级代码必须顶行写,即如果一行代码本身不依赖于任何条件,那它必须不能进行任何缩进 同一级别的代码,缩进必须一致 官方建议缩进用4个空格 Python程序语言指定任何非0和非空的布 ...
- LVS小型系统架构搭建笔记
搭建环境说明 本次实现用到了6台节点,实现一个小型的Lvs负载调度 节点1客户端配置代表互联网用户 [root@centos7 network-scripts]# vi ifcfg-ens37 TYP ...
- 简明 ASP.NET Core 手册2018
https://windsting.github.io/little-aspnetcore-book/book/ 中文版 https://nbarbettini.gitbooks.io/little- ...
- 【PCA】周志华
一.书籍
- oracle sql 游标的简单用法(tip:sql中两个单引号表示一个单引号)
--游标遍历某个字段 (打印出来) declare res_sql varchar2(2000); cursor cur is select f_dcnam ...
- 快学Scala 2
控制结构和函数 1.在Scala中,几乎所有构造出来的语法结构都有值.这个特性是为了使得程序更加精简,也更易读. (1)if表达式有值 (2)块也有值——是它最后一个表达式的值 (3)Scala的fo ...