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 ...
随机推荐
- 转:JAVA中解决Filter过滤掉css,js,图片文件等问题
原文链接:https://www.cnblogs.com/ermeng/p/6440966.html public void doFilter(ServletRequest request, Serv ...
- day3_字符串常用方法
s.upper()s.lower()s.capitalize()s.split(',')s.strip('abc')s.lstrip()s.rstrip()s.replace('old','new') ...
- Appium入门(6)__appium-desktop安装
部分摘自:http://www.testclass.net/appium/appium-base-desktop/ Appium-Server主要用来监听移动设备,然后将不同编程语言编写的 appiu ...
- 什么是HDFS
HDFS是什么:HDFS即Hadoop分布式文件系统(Hadoop Distributed Filesystem),以流式数据访问模式来存储超大文件,运行于商用硬件集群上,是管理网络中跨多台计算机存储 ...
- IQ调制原理
现代通信中,IQ调制基本上属于是标准配置,因为利用IQ调制可以做出所有的调制方式. 但是IQ调制到底是怎么工作的,为什么需要星座映射,成型滤波又是用来干嘛的.这个呢,讲通信原理的时候倒是都会泛泛的提到 ...
- FTP文件传输
FTP项目作业要求:1.用户加密认证2.允许同时多用户登录3.每个用户有自己的家目录,且只能访问自己的家目录4.对用户进行磁盘配额,每个用户的可用空间不同5.允许用户在ftp server上随意切换目 ...
- wxPython:事件处理一
事件处理是wxPython程序工作的基本机制,先看几个术语: 事件(event):应该程序期间发生的事情,要求有一个响应. 事件对象(event object):代表具体一个事件,包括事件的数据属性, ...
- js将字符串转换成json的三种方式
1,js自带的eval函数,其中需要添加小括号eval('('+str+')'); function strToJson(str){ var json = eval('(' + str + ')'); ...
- Observer(__ob__: Observer) 对象添加属性
重点通过这句话给对象添加属性: this.$set(r,'upshow',false); 在data中定义laws来装从接口中请求到数据 data(){ return{ laws:[],//法律依据 ...
- [django]django查询最佳实战
from django.db.models import Max, Min, Sum, Avg, Count, Q, F Django中的F和Q函数 一.F介绍 作用:操作数据表中的某列值,F()允许 ...