(hdu)5423 Rikka with Tree (dfs)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5423
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 tree T, let F(T,i) be the distance between vertice and vertice i.(The length of each edge is ). 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 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 testcases. For each testcase, the first line contains a number n(≤n≤). Then n− lines follow. Each line contains two numbers u,v(≤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
YES
NO
题意:两棵树中所有点到根节点的距离都相同时这两棵树叫做相似的树,两棵树中有某个节点的父节点不同时这两棵树叫不同的数,如果一棵树不存在另一棵树与之不同且相似则称这棵树为特殊的树,给出一棵树判断是否为特殊的树
方法:先分层,在判断是一条直树,还是扫帚型的
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <math.h>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
#define met(a,b) memset(a,b,sizeof(a));
#define N 1034
int Map[N][N];
int con[N];
int n;
void bfs(int s,int m)
{
con[s]=m;
for(int i=;i<=n;i++)
{
if(Map[s][i]&&!con[i])
{
bfs(i,m+);
}
}
}
int main()
{
int a,b;
int sum[N];
while(scanf("%d",&n)!=EOF)
{
met(Map,);met(con,);
for(int i=;i<n;i++)
{
scanf("%d %d",&a,&b);
Map[a][b]=Map[b][a]=;
}
bfs(,);///对树分层
met(sum,);int ans=;
for(int i=;i<=n;i++)
{
sum[con[i]]++;
}
for(int i=;i<=n;i++)
{
if(sum[i]==)///是否是一条线到底
ans++;
else
{
if(sum[i+]==)///是否是扫帚型
ans=n;
break;
}
}
if(ans==n)
printf("YES\n");
else
printf("NO\n");
}
return ;
}
(hdu)5423 Rikka with Tree (dfs)的更多相关文章
- hdu 5423 Rikka with Tree(dfs)
Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...
- hdu 5423 Rikka with Tree(dfs)bestcoder #53 div2 1002
题意: 输入一棵树,判断这棵树在以节点1为根节点时,是否是一棵特殊的树. 相关定义: 1. 定义f[A, i]为树A上节点i到节点1的距离,父节点与子节点之间的距离为1. 2. 对于树A与树B,如 ...
- HUD5423 Rikka with Tree(DFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5423 Rikka with Tree Time Limit: 2000/1000 MS (Java/O ...
- HDU 1325 Is It A Tree?(并查集)
题目大意: 给你两个节点,前者指向后者(可以认为前者是后者的父节点),然后让你判断是否是一棵树. 解题思路: 先说说这道题和小希的迷宫(HDU1272)那道题的区别,前者给出的两个点是有方向的,而后者 ...
- HDU 5423:Rikka with Tree Dijkstra算法
Rikka with Tree Accepts: 207 Submissions: 815 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- HDU 6191 Query on A Tree(可持久化Trie+DFS序)
Query on A Tree Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Othe ...
- 【HDU 4408】Minimum Spanning Tree(最小生成树计数)
Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...
- [ACM] HDU 5086 Revenge of Segment Tree(全部连续区间的和)
Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...
- HDU 6191 Query on A Tree(字典树+离线)
Query on A Tree Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Othe ...
随机推荐
- C# 函数覆盖总结学习
覆盖类成员:通过new关键字修饰虚函数表示覆盖该虚函数.一个虚函数被覆盖后,任何父类变量都不能访问该虚函数的具体实现.public virtual void IntroduceMyself(){... ...
- cardsui-for-android
https://github.com/Androguide/cardsui-for-android cardsui-for-android-master.zip
- linux 下网站压力测试工具webbench
一直在用webbench ,这个linux下的网站压力测试工具.整理下. 笔记本装的ubuntu,其他linux系统也差不多. webbench 需要先安装 ctags,一个vim的阅读插件,可以直接 ...
- SQL Server 数学函数 相关
1.计算绝对值ABS ABS函数对一个数值表达式结果计算绝对值(bit数据类型除外),返回整数. 语法结构: ABS(数值表达式) 返回值:与数值表达式类型一致的数据 示例: SELECT ABS ...
- Linux系统如何平滑生效NAT-BUGFIX
在< Linux系统如何平滑生效NAT>中,代码有两处问题.这只是目前发现的,没有发现的还有很多很多,这就是我为何不一开始把代码搞复杂的原因. 1.一个bug附带一个优化: 注意以下的代码 ...
- PHP.10-PHP实例(一)-简单的计算器
PHP-简单的计算器 [PHP语法详解] PHP在web开发中的应用 PHP编写步骤 1.编写一个后缀名为.php文件2.上传到Web服务器的文档根目录下3.通过浏览器访问Web服务器管理下的PHP文 ...
- 疑问-hadoop
hadoop可以设置备份数据个数,那么这些不同节点的数据块对于hadoop来说有区分谁是备份的谁是直接可以用的吗? hadoop有机架策略,给某些操作如像根据带宽选择节点传输数据 那么这种策略的根据是 ...
- The Famous Clock
描述 Mr. B, Mr. G and Mr. M are now in Warsaw, Poland, for the 2012’s ACM-ICPC World Finals Contest. T ...
- SQL把做个字段组合成一个字符串
- s实现指定时间自动跳转到某个页面
--js实现指定时间自动跳转到某个页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &quo ...