codeforces Bear and Drawing
来源:互联网 发布:淘宝专业差评 编辑:程序博客网 时间:2024/04/29 07:49
题目链接:http://codeforces.com/problemset/problem/573/C
题意:
有一棵n个点的树, 然后有两行列无限点的, 问这棵树能否画出。 能输出Yes, 否则输出No。
分析:
对于一个点,它衍生出来的最左端的点和最右端的点可以衍生出 >2个点。除了最左边和最右边的点, 最多只能衍生出2个点, 而这些点只能衍生一个点。则对于每个连接数>2的点,最多只有两个点的连接数>=3.
AC代码:
#include<stdio.h>#include<string.h>#include<vector>#include<algorithm>using namespace std;const int maxn = 1e5 + 10;vector<int> G[maxn];int dg[maxn], d[maxn], vis[maxn];void dfs(int u){ vis[u] = 1; for(int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if(!vis[v] && dg[v]<=2) dfs(v); }}int main(){ int n, a, b, cnt; bool flag; scanf("%d", &n); memset(vis, 0, sizeof(vis)); memset(dg, 0, sizeof(dg)); memset(d, 0, sizeof(d)); for(int i = 0; i <= n; i++) G[i].clear(); for(int i = 1; i < n; i++) { scanf("%d%d", &a, &b); dg[a]++; dg[b]++; G[a].push_back(b); G[b].push_back(a); } for(int i = 1; i <= n; i++) if(dg[i] == 1) dfs(i); for(int i = 1; i <= n; i++) if(!vis[i]) { for(int j = 0; j < G[i].size(); j++) { int v = G[i][j]; if(vis[v]) d[i] = min(d[i]+1, 2); } } flag = true; for(int i = 1; i <= n; i++) if(!vis[i]) { cnt = 0; for(int j = 0; j < G[i].size(); j++) { int v = G[i][j]; if(!vis[v] && dg[v]-d[v]>1) cnt++; } if(cnt > 2) { flag = false; break; } } if(flag) puts("Yes"); else puts("No"); return 0;}
0 0
- codeforces Bear and Drawing
- CodeForces #318 (div1) C.Bear and Drawing
- codeforces 573C. Bear and Drawing
- Codeforces Round #318 (Div. 2) E - Bear and Drawing
- codeforces Bear and Raspberry
- Bear and Poker CodeForces
- Bear and Elections CodeForces
- 【codeforces】 Bear and Reverse Radewoosh
- 【codeforces】Bear and Three Balls
- codeforces Bear and Friendship Condition
- codeforces Bear and Different Names
- Bear and Friendship Condition CodeForces
- Bear and Three Balls CodeForces
- Codeforces Bear and Big Brother
- Bear and Three Musketeers CodeForces
- Codeforces 385 C. Bear and Prime Numbers
- CodeForces 385C Bear and Prime Numbers
- CodeForces 385D Bear and Floodlight
- 开发者必备的十八大Android开发资源
- ViewPager的使用(三)-viewPager与Fragment配合使用
- Java中数组和集合的区别
- iOS开发:一个瀑布流的设计与实现(已实现缓存池功能,该功能使得瀑布流cell可以循环利用)
- Android 使用动态加载框架DL进行插件化开发
- codeforces Bear and Drawing
- C#递归批量清空TEXTBOX
- 菜鸟系列——八数码八境界
- push到github时,每次都要输入用户名和密码的问题
- C程序设计语言(第2版)
- CentOS 6.3安装NodeJS环境
- 测试面试-理论
- C文件读取
- signal函数