hdu 5423 Rikka with Tree(思路)
2017-04-12 14:10
357 查看
Rikka with Tree
Problem DescriptionAs 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
Hint
For the second testcase, this tree is similiar with the given tree:
4
1 2
1 4
3 4
Source
BestCoder Round #53 (div.2)
ps:这种题理解了题意应该都会做,关键就是题意真的难看出来。。。。。。
题意:
对于相似的定义:两棵树有相同数量的节点数并且每一个节点到根的深度都相同
对于不同的定义:两棵树有不同数量的节点数或者当1为根结点时,存在一个节点i在A和B中的父亲不同
对于特殊的定义:对于树A,不存在一棵树B使得A和B既相似又不同
问给你的这棵树是不是特殊的树
思路:
如果一个棵树是特殊的树,那么它必定有以下
4000
性质:
1.这棵树最多只有一个节点有多个儿子
2.如果这棵树的一个节点有了多个儿子,那么它肯定不会有孙子
所以我们只需要深搜一下判断有多个儿子的那个节点是否有孙子就好了
代码:
#include<stdio.h> #include<vector> using namespace std; #define maxn 1002 int n,flag; vector<int>q[maxn]; void dfs(int x,int flog) { if(flag||!q[x].size()) return ; if(flog&&q[x].size()>0) { flag=1; return ; } if(q[x].size()>1) { for(int i=0; i<q[x].size(); ++i) { dfs(q[x][i],1); } } else dfs(q[x][0],0); } int main() { while(~scanf("%d",&n)) { flag=0; for(int i=1; i<=n; ++i) q[i].clear(); int u,v; for(int i=0; i<n-1; ++i) { scanf("%d%d",&u,&v); q[u].push_back(v); } dfs(1,0); if(!flag) printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- hdu 5423 Rikka with Tree(思路)
- hdu 5423 Rikka with Tree(思路)
- hdu 5423 Rikka with Tree(思路)
- hdu 5423 Rikka with Tree(思路)
- hdu 5423 Rikka with Tree(思路)
- hdu 5423 Rikka with Tree(思路)
- hdu 5423 Rikka with Tree(思路)
- hdu 5423 Rikka with Tree(思路)
- hdu 5423 Rikka with Tree(思路)
- hdu 5423 Rikka with Tree(思路)
- hdu 5423 Rikka with Tree(思路)
- hdu 5423 Rikka with Tree(思路)
- hdu 5423 Rikka with Tree(思路)
- HDU~5423 Rikka with Tree(思路+搜索)
- hdu 5423 Rikka with Tree DFS
- Rikka with Tree HDU - 5423
- HDU 5423 Rikka with Tree(bfs or dfs)——BestCoder Round #53(div.1 div.2)
- HDU 5423 Rikka with Tree(简单DFS+枚举)
- hdu 5423 Rikka with Tree 乱搞
- HDU-5423 Rikka with Tree。树深搜