HDU 5423 Rikka with Tree(水题)
来源:互联网 发布:零用钱软件赚钱靠谱吗 编辑:程序博客网 时间:2024/06/06 07:34
Rikka with Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 292 Accepted Submission(s): 149
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)
中文题意:
问题描述
众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的:对于一棵树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"。
#include<iostream>#include<algorithm>#include<cstdio>#include<string.h>#include<stdlib.h>using namespace std;int n,m;int v[1010];int map[1001][1001];int main() { while(scanf("%d",&n)!=EOF) { memset(map,0,sizeof(map)); memset(v,0,sizeof(v)); int x,y; int pf; for(int i=0; i<n-1; i++) { scanf("%d%d",&x,&y); map[x][y] = 1; map[y][x] = 1; if(x == 1) { pf = y; } else if(y == 1) { pf = x; } v[x]++; v[y]++; } int rt = 0; int cnt = 0,ans = 0,pt = 0; for(int i=1; i<=n; i++) { if(v[i] == 1) { ans++; } else if(v[i] == 2) { cnt++; }else{ rt = i; pt++; } } if(pt>=2){ printf("NO\n"); continue; } if(v[1]!=1) { if(v[1] == n-1) { printf("YES\n"); } else { printf("NO\n"); } }else{ if(cnt + ans +1 >= n && v[pf] <=2){ int flag = 0; if(pt>0){ for(int i=1;i<=n;i++){ if(map[rt][i] == 1 && v[i]!=1){ flag++; } } } if(flag <= 1){ printf("YES\n"); }else{ printf("NO\n"); } }else{ if(v[pf] == n-1){ printf("YES\n"); continue; } printf("NO\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
- 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 DFS
- hdu 5423 Rikka with Tree(dfs)
- HDU 5423 Rikka with Tree(DFS)
- 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 树的性质
- django 1.8 官方文档翻译: 3-2-3 TemplateResponse 和 SimpleTemplateResponse
- Android-手势识别(普通手势识别:上 下 左 右 ;自定义手势识别:对号,错号 等)
- STL源码剖析-配置器(内存分配器)
- 计算机组成原理四:存储器
- XML的优势及应用领域
- HDU 5423 Rikka with Tree(水题)
- Android网络编程之使用post方式提交数据
- 遍历ArrayList删除指定元素
- TreeSet
- Set up Caffe on Ubuntu14.04 64bit+NVIDIA GTX970M+CUDA7.0
- Generate Parentheses
- Autoit3 安装
- 图的割点
- 从图片数据获取高度宽度