题目链接: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)的更多相关文章

  1. hdu 5423 Rikka with Tree(dfs)

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...

  2. hdu 5423 Rikka with Tree(dfs)bestcoder #53 div2 1002

    题意: 输入一棵树,判断这棵树在以节点1为根节点时,是否是一棵特殊的树. 相关定义: 1.  定义f[A, i]为树A上节点i到节点1的距离,父节点与子节点之间的距离为1. 2.  对于树A与树B,如 ...

  3. HUD5423 Rikka with Tree(DFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5423 Rikka with Tree Time Limit: 2000/1000 MS (Java/O ...

  4. HDU 1325 Is It A Tree?(并查集)

    题目大意: 给你两个节点,前者指向后者(可以认为前者是后者的父节点),然后让你判断是否是一棵树. 解题思路: 先说说这道题和小希的迷宫(HDU1272)那道题的区别,前者给出的两个点是有方向的,而后者 ...

  5. HDU 5423:Rikka with Tree Dijkstra算法

    Rikka with Tree  Accepts: 207  Submissions: 815  Time Limit: 2000/1000 MS (Java/Others)  Memory Limi ...

  6. 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 ...

  7. 【HDU 4408】Minimum Spanning Tree(最小生成树计数)

    Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...

  8. [ACM] HDU 5086 Revenge of Segment Tree(全部连续区间的和)

    Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...

  9. HDU 6191 Query on A Tree(字典树+离线)

    Query on A Tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Othe ...

随机推荐

  1. Codeforces Round #280 (Div. 2) E. Vanya and Field 数学

    E. Vanya and Field Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/492/pr ...

  2. android 几个小技巧

    1.如果打开模拟器,不同程序打开了不同的模拟器.可能是某个某个模拟器的target版本过低,修改一下4.2,应该都可以用了 2.找不到R.id.的错误,不妨删除menu文件夹下的xml文件 3.act ...

  3. org.apache.hadoop.fs-Seekable

    本来要先看BufferedFSInputStream的,但是它实现了Seekable和PositionedReadable接口,就先看这两个,再看它会比较容易理解些 package org.apach ...

  4. Mac 开发者的十八般兵器:重温 10 篇热文

    <开发者 MAC 电脑里的十八般兵器> 古人常以刀.枪.剑.戟.斧.钺.铲.叉.鞭.锏.锤.戈.镋.棍.槊.棒.矛.钯十八种兵器,样样精通,来形容一个人的武学技能get状态.在开发者的世界 ...

  5. mysql单引号和双引号的用法

    表名,列名最好用`(esc下面那个,不用`会出错) 这就要从双引号和单引号的作用讲起:双引号里面的字段会经过编译器解释然后再当作HTML代码输出,但是单引号里面的不需要解释,直接输出.例如:$abc= ...

  6. Hashtable 和 HashMap 的比较

        Hashtable HashMap 并发操作 使用同步机制, 实际应用程序中,仅仅是Hashtable本身的同步并不能保证程序在并发操作下的正确性,需要高层次的并发保护. 下面的代码试图在ke ...

  7. mysql 慢执行分析工具explain/desc

    一.如何操作 explain + 增删改查语句; 二. 输出格式 possible_keys  mysql在搜索表记录时可能使用哪个索引. key  实际使用的索引,如果没有索引被使用,则为null. ...

  8. arguments的用法

    arguments 对象 在函数代码中,使用特殊对象 arguments,开发者无需明确指出参数名,就能访问它们. 例如,在函数 sayHi() 中,第一个参数是 message.用 argument ...

  9. linq检索带命名空间的xml

    XElement el = XElement.Load(fil); XNamespace ns = "http://schemas.microsoft.com/ado/2009/11/edm ...

  10. Git CMD - config: Get and set repository or global options

    命令参数 --get 获取指定的配置项. --global 对于写选项:全局配置,将参数配置于 ~/.gitconfig 而不是仓库目录下的 .git/config.对于读选项:只从 ~/.gitco ...