HDU~5423 Rikka with Tree(思路+搜索)
来源:互联网 发布:ubuntu pyqt 安装 编辑:程序博客网 时间:2024/06/08 13:27
Rikka with Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 893 Accepted Submission(s): 409
Problem 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 treeT , let F(T,i) be the distance between vertice 1 and vertice i .(The length of each edge is 1).
Two treesA 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 treesA 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.
TreeA 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?
For a tree
Two trees
Two trees
Tree
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 numbern(1≤n≤1000) .
Thenn−1 lines follow. Each line contains two numbers u,v(1≤u,v≤n) , which means there is an edge between u and v .
For each testcase, the first line contains a number
Then
Output
For each testcase, if the tree is special print "YES" , otherwise print "NO".
Sample Input
31 22 341 22 31 4
Sample Output
YESNOHintFor the second testcase, this tree is similiar with the given tree:41 21 43 4
Source
BestCoder Round #53 (div.2)
Recommend
hujie
搬大佬题意
from:http://blog.csdn.net/blesslzh0108/article/details/70142256
题意:
对于相似的定义:两棵树有相同数量的节点数并且每一个节点到根的深度都相同
对于不同的定义:两棵树有不同数量的节点数或者当1为根结点时,存在一个节点i在A和B中的父亲不同
对于特殊的定义:对于树A,不存在一棵树B使得A和B既相似又不同
问给你的这棵树是不是特殊的树
思路:
如果一个棵树是特殊的树,那么它必定有以下性质:
1.这棵树最多只有一个节点有多个儿子
2.如果这棵树的一个节点有了多个儿子,那么它肯定不会有孙子
所以我们只需要深搜一下判断有多个儿子的那个节点是否有孙子就好了
#include <iostream>#include<stdio.h>#include<string>#include<string.h>#include<algorithm>#include<math.h>#include<queue>#include<stdlib.h>#include<stdio.h>#include<map>#include<vector>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define ll long long#define N 1005#define mod 10000007using namespace std;int n,flag;vector<int>v[N];void dfs(int x,int flog){ if(flag||!v[x].size()) return; if(flog&&v[x].size()>0) { flag=1; return; } if(v[x].size()>1) { for(int i=0;i<v[x].size();i++) dfs(v[x][i],1); } else dfs(v[x][0],0);}int main(){ while(~scanf("%d",&n)) { for(int i=1;i<=n;i++) v[i].clear(); flag=0; int x,y; for(int i=1;i<n;i++) { scanf("%d%d",&x,&y); v[x].push_back(y); } dfs(1,0); if(flag) printf("NO\n"); else printf("YES\n"); } return 0;}
0 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
- Rikka with Tree HDU
- HDU 5423 Rikka with Tree(构造)
- HDU 5423:Rikka with Tree Dijkstra算法
- HDU 5423 Rikka with Tree(水题)
- hdu 5423 Rikka with Tree 乱搞
- hdu 5423 Rikka with Tree(深搜)
- hdu 5423 Rikka with Tree DFS
- hdu 5423 Rikka with Tree(dfs)
- HDU 5423 Rikka with Tree(DFS)
- hdu 5423 Rikka with Tree(水)
- HDU-5423 Rikka with Tree。树深搜
- hdu 6092 Rikka with Subset(思路)
- [机器学习]机器学习笔记整理14-多元回归分析及实现
- 做微商可享社保补贴?微商的市场行情发展怎么样?
- 安卓6.0权限适配 ----RxPermissions
- HTML4 与 HTML5 的区别
- This version of Android Studio is incompatible with the Gradle Plugin used解决
- HDU~5423 Rikka with Tree(思路+搜索)
- Ubuntu 14.04 触摸板不能使用的解决办法
- 逆元求法
- SpringBoot 事务的应用
- mysql中没有提交的事务查询
- Linux 显示文件的某几行
- kubernetes(3):Centos7 安装 kubernetes admin 版本
- 数据结构与算法分析之AVL平衡树
- Android启动模式及Intent属性----重新认识Android(5)