hdu 5423 树上dfs 水题
2015-08-30 12:12
423 查看
给你一颗树,问是否存在节点深度全部相同但存在某个节点的父亲不同的
求每一个深度的节点个数,只要存在一个深度有超过一个的节点并且下一个深度有节点存在,就满足了上述的条件
求每一个深度的节点个数,只要存在一个深度有超过一个的节点并且下一个深度有节点存在,就满足了上述的条件
#include<stdio.h> #include<string> #include<map> #include<vector> #include<cmath> #include<stdlib.h> #include<string.h> #include<algorithm> #include<iostream> using namespace std; #define rep(i,n) for(int i=0;i<n;i++) const int N=1e3+10; const int MOD=1e9+7; int n,m,k,up; int dep ; int cnt ; vector<int> G ; void dfs(int u,int f){ for(int i=0;i<G[u].size();i++){ int v=G[u][i]; if(v==f) continue; dep[v]=dep[u]+1; cnt[dep[v]]++; dfs(v,u); } } int main(){ #ifndef ONLINE_JUDGE freopen("aaa","r",stdin); #endif int T; while(~scanf("%d",&n)){ int u,v; rep(i,n) G[i].clear(); memset(cnt,0,sizeof cnt); rep(i,n-1) cin>>u>>v,G[u-1].push_back(v-1),G[v-1].push_back(u-1); dep[0]=0; cnt[0]=1; dfs(0,-1); bool ok=false; rep(i,n) ok|= cnt[i]>1 && cnt[i+1]>0; cout<<(ok?"NO":"YES")<<endl; } return 0; }
相关文章推荐
- Xcode快捷键及 Unix 指令
- Redis 命令行 常用总结
- 较安全的rm脚本
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) A. Bear and Elections(优先队列)
- 【Python】笔记:类继承问题总结
- 树莓派(RespberryPi)安装手记
- tcpdump的基本参数说明
- Gradle 代理设置
- 安装完最小化RHEL/CentOS 7后需要做的30件事情
- JSP的静态包含和动态包含
- 脚本的响应事件与组件访问
- linux常用命令随记
- 又见大雨声,想起小雨,还有那条小街
- JS对象的写法
- Bear and Three Musketeers
- Linux系统--Linux程序包管理
- ssh 登录出现的几种错误以及解决办法
- CentOS 安装 Hadoop 手记
- eclipse设置
- [LeetCode#128]Word Ladder II