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 ...
随机推荐
- Linux下MySql的配置文件my.cnf详细 讲解
经常在使用MySql,但是对于MySql下面的各种参数的配置并不是很熟悉,经常在需要改变某项参数的时候,还要到处在网上查找,有点不方便.今天想把MySql下面的配置文件my.cnf详细的做一个说明(L ...
- 我的grunt学习笔记
什么是grunt? Grunt是一个JavaScript任务运行器,用于自动执行频繁任务(如压缩,编译,单元测试)的工具.它使用命令行界面来运行在文件中定义的自定义任务(这个文件称为Gruntfil ...
- 重读《深入理解Java虚拟机》三、Java虚拟机执行的数据入口(类文件结构)
1.Java如何实现平台无关系 Java要实现平台无关系就需要在Java代码和本地机器之间引入一个中间层,实现Java代码和本地机器的解耦,而这个中间层就是字节码.字节码独立于本地机器,以实现代码的可 ...
- 关于linux特殊含义的转义符\033
格式: echo -e "\033[字背景颜色;字体颜色m字符串\033[0m" 例如: echo -e "\033[41;36m something here \033 ...
- java定时任务的三种方式
/** * 普通thread * 这是最常见的,创建一个thread,然后让它在while循环里一直运行着, * 通过sleep方法来达到定时任务的效果.这样可以快速简单的实现,代码如下 */ ...
- 实现web消息推送的技术和采用长轮询corundumstudio介绍
实时消息的推送,PC端的推送技术可以使用socket建立一个长连接来实现.传统的web服务都是客户端发出请求,服务端给出响应.但是现在直观的要求是允许特定时间内在没有客户端发起请求的情况下服务端主动推 ...
- 在字符编码格式选项里UTF-8(无BOM)
BOM——Byte Order Mark,就是字节序标记 在UCS 编码中有一个叫做”ZERO WIDTH NO-BREAK SPACE“的字符,它的编码是FEFF.而FFFE在UCS中是不存在的字符 ...
- nginx 启动报错403
nginx 安装完成以后启动的时候报403, 网上找的答案是在配置文件nginx.conf里面加上 user root owner;这个要加在配置文件的第一行才行,否则还是会报错,配置文件截图为: 参 ...
- 小程序图片保存的相册授权失败问题,原接口wx.openSeting()已经废弃了的解决办法
项目中使用的是wepy框架开发的小程序,,, 使用场景是,用户点击下载图片的时候,要调起授权button(小程序拉起授权的功能都集成在了button组件,所以这里需要用到button组件里的一个ope ...
- ps切图插件
ps切图工具插件 下载网址:http://www.cutterman.cn/zh/cutterman 安装完插件,关闭ps,重新打开,窗口- 扩展,即可使用