E. Bear and Drawing
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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?

Input

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.

Output

Print "Yes" (without the quotes) if Limak can draw chosen tree. Otherwise, print "No" (without the quotes).

Sample test(s)
input
8
1 2
1 3
1 6
6 4
6 7
6 5
7 8
output
Yes
input
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
output
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的更多相关文章

  1. codeforces 573C Bear and Drawing

    Limak is a little bear who learns to draw. People usually start with houses, fences and flowers but ...

  2. CF573C Bear and Drawing 构造+树论

    正解:构造 解题报告: 传送门! 这题首先可以画下图找下规律,,,然后通过找规律可以发现,最终的方案一定是一条主干+一些枝条,而且这些枝条的分杈一定小于等于2 明确一下主干的定义,最左边的节点和最右边 ...

  3. Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) C. Bear and Drawing

    题目链接:http://codeforces.com/contest/573/problem/C题目大意:在两行无限长的点列上面画n个点以及n-1条边使得构成一棵树,并且要求边都在同一平面上且除了节点 ...

  4. Codeforces Round #318(Div 1) 573A, 573B,573C

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 这场的前两题完全是手速题...A题写了7分钟,交的时候已经500+了,好在B题出的 ...

  5. Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)

    以后每做完一场CF,解题报告都写在一起吧   暴力||二分 A - Bear and Elections 题意:有n个候选人,第一个候选人可以贿赂其他人拿到他们的票,问最少要贿赂多少张票第一个人才能赢 ...

  6. D. Bear and Two Paths(贪心构造)

    D. Bear and Two Paths time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  7. 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 ...

  8. 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 ...

  9. Codefoces 791D. Bear and Tree Jumps 树形DP

    D. Bear and Tree Jumps   A tree is an undirected connected graph without cycles. The distance betwee ...

随机推荐

  1. eclipse几种常见问题的解决

    build项目时出现卡死现象的解决方案 场景:在使用使用Eclipse编辑文件保存时或者build项目时,经常出现卡死现象,此时即便杀死eclipse进程重启还是依然出现这种现象. 原因:eclips ...

  2. 三、Spring Boot 日志

    1.日志框架 小张:开发一个大型系统: 1.System.out.println(""):将关键数据打印在控制台:去掉?写在一个文件? 2.框架来记录系统的一些运行时信息:日志框架 ...

  3. SQL 2016 AlwaysOn 无域AlwaysOn配置要点

    环境准备: (1)操作系统:Windows Server 2016 Datacenter (2)SQL Server 2016 配置SQL  2016 AlwaysOn 要点 1,因为没有域所以需要在 ...

  4. 启动Jmeter4.0 后弹出警告: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0 x80000002. Windows RegCreateKeyEx(...) returned error code 5.

    启动Jmeter4.0 后弹出命令窗口提示信息: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at r ...

  5. Postman教程

    1.Postman的介绍 Postman是一款功能强大的用于发送 HTTP 请求的 Chrome插件 .做web页面开发和测试的人员应该都知道!其主要特点:创建 + 测试:创建和发送任何的HTTP请求 ...

  6. vue中less的使用

    1.安装:npm install less less-loader --save 2.修改webpack.config.js文件,配置loader加载依赖,让其支持外部的less,在原来的代码上添加 ...

  7. vue 本地静态图片路径写法

    在img标签中 <img src="../../assets/images/haibao/06-2@2x.png" class="" width=&quo ...

  8. Linux ssh将命令放入后台

    如何在关闭ssh连接的情况下,让程序继续运行? 对Unix,Linux类服务器维护经常是通过ssh完成的,而有些操作比较费时,如更新程序等.此时如果断开ssh连接的话,更新程序就会随之被中断.如何保证 ...

  9. 使用autoconf与automake自动生成MakeFile文件

    automake主要通过编辑Makefile.am来控制它的行为,下面就常用的三个Makefile.am配置做出说明. 1.1. autotools的工作原理 autotools最终是为了生成Make ...

  10. nodejs+mysql入门实例(增)

    var userAddSql = 'INSERT INTO userinfo(id,username,pwd) VALUES(0,?,?)'; var userAddSql_Params = ['Wi ...