hdu 5423 统计树每一层的节点树 dfs
2015-08-30 15:57
417 查看
roblem Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:
For a tree T,
let F(T,i) be
the distance between vertice 1 and vertice i.(The
length of each edge is 1).
Two trees A and B are
similiar if and only if the have same number of vertices and for each i meet F(A,i)=F(B,i).
Two trees A and B are
different if and only if they have different numbers of vertices or there exist an number i which
vertice i have
different fathers in tree A and
tree B when
vertice 1 is root.
Tree A is
special if and only if there doesn't exist an tree B which A and B are
different and A and B are
similiar.
Now he wants to know if a tree is special.
It is too difficult for Rikka. Can you help her?
Input
There are no more than 100 testcases.
For each testcase, the first line contains a number n(1≤n≤1000).
Then n−1 lines
follow. Each line contains two numbers u,v(1≤u,v≤n) ,
which means there is an edge between u and v.
Output
For each testcase, if the tree is special print "YES" , otherwise print "NO".
Sample Input
Sample Output
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:
For a tree T,
let F(T,i) be
the distance between vertice 1 and vertice i.(The
length of each edge is 1).
Two trees A and B are
similiar if and only if the have same number of vertices and for each i meet F(A,i)=F(B,i).
Two trees A and B are
different if and only if they have different numbers of vertices or there exist an number i which
vertice i have
different fathers in tree A and
tree B when
vertice 1 is root.
Tree A is
special if and only if there doesn't exist an tree B which A and B are
different and A and B are
similiar.
Now he wants to know if a tree is special.
It is too difficult for Rikka. Can you help her?
Input
There are no more than 100 testcases.
For each testcase, the first line contains a number n(1≤n≤1000).
Then n−1 lines
follow. Each line contains two numbers u,v(1≤u,v≤n) ,
which means there is an edge between u and v.
Output
For each testcase, if the tree is special print "YES" , otherwise print "NO".
Sample Input
3 1 2 2 3 4 1 2 2 3 1 4
Sample Output
YES NO HintFor the second testcase, this tree is similiar with the given tree: 4 1 2 1 4 3 4 如果一个节点有兄弟节点还有儿子,那么把儿子节点挪到兄弟节点上则他就是有相似的树,所以,我们只有当每一个节点只有一个儿子,叶子节点除外,最后一层可以有多个节点则无这棵树相似的树,也就是 一条串 。-。-。-。-。-。最后一层可有多个叶子节点,#include<bits/stdc++.h> using namespace std; int maxdepth = 0; int m[1005][1005]; int d[1005]; int cnt[1005]; int n; void dfs(int u ,int depth) { maxdepth = max(depth,maxdepth); d[u] = depth; for(int i = 1; i<= n; i++) { if(d[i]==-1&&m[u][i]) { dfs(i,depth+1); } } } void Init() { memset(d,-1,sizeof(d)); memset(m,0,sizeof(m)); memset(cnt,0,sizeof(cnt)); maxdepth = 0; } int main() { while(scanf("%d",&n)!=EOF) { Init(); int a,b; for(int i = 1 ; i < n; i++) { scanf("%d%d",&a,&b); m[a][b]=1; m[b][a]=1; } dfs(1,1); for(int i = 1; i<= n; i++) { cnt[d[i]]++; } int flag = 0; for(int i = 1 ; i <maxdepth; i++) { if(cnt[i]>1) { flag = 1; break; } } if(!flag) printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- 鸡兔同笼
- iOS 状态栏(UIStatusBar)的相关设置
- [HDOJ1754]I Hate It
- 插入排序
- PhantomJS简介
- 面试题5:从尾到头打印链表
- 关于js的Array.prototype.slice.call
- 多线程与RunLoop
- java输入输出流
- Recommending music on Spotify with deep learning 采用深度学习算法为Spotify做基于内容的音乐推荐
- 面试题29.数组中出现次数超过一半的数字
- 如何使用贝瑟尔曲线切的圆角 Button
- AddressBook/AddressBookUI
- 了解FTP
- java实现map和object互转的三种方法
- NGUI中Panel渲染顺序记录
- hihocoder 1164 随机斐波那契 (期望dp)
- Swap Nodes in Pairs
- Android(java)学习笔记203:网页源码查看器(Handler消息机制)
- Linux命令汇总之用户和组的相关命令