Rikka with Tree
来源:互联网 发布:win7运行xp软件 编辑:程序博客网 时间:2024/06/05 03:07
Rikka with Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 886 Accepted Submission(s): 405
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 numberi 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中文翻译:众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的:对于一棵树T,令F(T,i)为点1到点i的最短距离(边长是1). 两棵树A和B是相似的当且仅当他们顶点数相同且对于任意的i都有F(A,i)=F(B,i).两棵树A和B是不同的当且仅当他们定点数不同或者存在一个i使得以1号点为根的时候i在两棵树中的父亲不同。一棵树A是特殊的当且仅当不存在一棵和它不同的树和它相似。现在勇太想知道一棵树到底是不是特殊的。当然,这个问题对于萌萌哒六花来说实在是太难了,你可以帮帮她吗?输入描述数据组数不超过100组。每组数据的第一行一个整数n(2≤n≤1000)。接下来n−1行。每行两个整数u,v(1≤u,v≤n),代表给定树上的一条边。输出描述对于每一组数据,如果给定树是特殊的输出"YES"否则输出"NO"。输入样例31 22 341 22 31 4输出样例YESNOHint对于第二组数据下面这棵树和它相似。41 21 43 4思路:显然一棵树是独特的当且仅当任意处于每一个深度的点数是"1 1 1 1 ... 1 1 x"。所以直接DFS一下求出每一个点到根的距离然后判断一下就好了#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<vector>using namespace std;vector<int>q[1005];vector<int>q1[1005];int vis[1005];int dep;void dfs(int dian,int depth){ q1[depth].push_back(dian);//把每一层的节点数记录下来,看看每一层有几个节点 vis[dian]=1; if(dian!=1&&q[dian].size()==1)//记录每个点与根节点的距离(即深搜的深度) { dep=max(dep,depth); return ; } for(int i=0;i<q[dian].size();i++) { if(!vis[q[dian][i]]) { dfs(q[dian][i],depth+1); vis[q[dian][i]]=1; } }}int judge(){ for(int i=1;i<dep;i++)//在小于深搜树的高度时枚举深搜树的每一层结点数,若中间某一层结点数大于1,则树是相似的 { //printf("***%d\n",q1[i].size()); if(q1[i].size()>1)//当每一层的节点数》1的时候那么就是相似的 { return 0; } } return 1;}int main(){ int n; while(scanf("%d",&n)!=-1) { int a,b; memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) { q[i].clear(); q1[i].clear(); } for(int i=0;i<n-1;i++) { scanf("%d%d",&a,&b); q[a].push_back(b); q[b].push_back(a); } dep=0; dfs(1,0); if(judge()) { printf("YES\n"); } else { printf("NO\n"); } }}
0 0
- hdu54235423 Rikka with Tree
- HDU5423 Rikka with Tree
- Rikka with Tree HDU
- hdu5423 Rikka with Tree
- 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 Dijkstra算法
- hdu5423 Rikka with Tree(DFS)
- 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)
- Problem F: 立体空间中的点(II)
- Android Studio升级2.3后Gradle 的配置问题,适用2.2、2.1
- PHP——实时显示当前时间
- Android studio安装
- 现实世界里的 SOA
- Rikka with Tree
- web简单基础,它是什么?
- Markdown中Latex 数学公式基本语法
- 【memcache缓存专题(2)】memcache安装与命令行使用
- 求和问题 Sum Problem
- String,StringBuffer,StringBuilder区别
- 求MySQL先按大于等于当前时间升序排序,再按小于当前时间降序排序
- fragment里面new一个Dialog报错
- 【memcache缓存专题(3)】PHP-memcache扩展的安装以及使用