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 1 and vertice i.(The length of each edge is 1).

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 1 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 100 testcases.

For each testcase, the first line contains a number n(1≤n≤1000).

Then n−1 lines follow. Each line contains two numbers u,v(1≤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
3
1 2
2 3
4
1 2
2 3
1 4
 
Sample Output
YES
NO

Hint

For the second testcase, this tree is similiar with the given tree:

4
1 2
1 4
3 4

题目要求的那个特殊的树其实就是一条直链,然后在链的末端接上若干节点,类似于扫帚的形状。

也就是最后一层的节点下方没有子节点,倒数第二层的节点下方可以有若干子节点,其余节点均只有一个子节点。

用dfs搜索每一层的节点进行判断即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <vector>
#define LL long long using namespace std; const int maxN = ; struct Edge
{
int to, next;
//int val;
}edge[maxN*]; int head[maxN], cnt; void addEdge(int u, int v)
{
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt;
cnt++;
} void initEdge()
{
memset(head, -, sizeof(head));
cnt = ;
} int n;
bool vis[maxN];
int maxDepth;
bool flag; bool input()
{
if (scanf("%d", &n) == EOF)
return false;
initEdge();
memset(vis, false, sizeof(vis));
maxDepth = ;
flag = true;
int u, v;
for (int i = ; i < n; ++i)
{
scanf("%d%d", &u, &v);
addEdge(u, v);
addEdge(v, u);
}
return true;
} void dfs(int now, int depth)
{
if (flag == false)
return;
int cnt = ;
vis[now] = true;
for (int i = head[now]; i != -; i = edge[i].next)
{
if (vis[edge[i].to])
continue;
dfs(edge[i].to, depth+);
cnt++;
}
if (cnt == )
return;
maxDepth = max(maxDepth, depth);
if (depth != maxDepth && cnt != )
flag = false;
} void work()
{
dfs(, );
if (flag)
printf("YES\n");
else
printf("NO\n");
} int main()
{
//freopen("test.in", "r", stdin);
while (input())
{
work();
}
return ;
}

ACM学习历程—HDU5423 Rikka with Tree(搜索)的更多相关文章

  1. ACM学习历程——HDU5202 Rikka with string(dfs,回文字符串)

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

  2. ACM学习历程—HDU 5534 Partial Tree(动态规划)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x ...

  3. ACM学习历程—HDU5422 Rikka with Graph(贪心)

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

  4. HDU-5423 Rikka with Tree。树深搜

    Rikka with Tree 题意:给出树的定义,给出树相似的定义和不同的定义,然后给出一棵树,求是否存在一颗树即和其相似又与其不同.存在输出NO,不存在输出YES. 思路:以1号节点为根节点,我们 ...

  5. ACM学习历程——POJ3321 Apple Tree(搜索,线段树)

          Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will ...

  6. ACM学习历程—POJ1088 滑雪(dp && 记忆化搜索)

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  7. ACM学习历程—ZOJ3471 Most Powerful(dp && 状态压缩 && 记忆化搜索 && 位运算)

    Description Recently, researchers on Mars have discovered N powerful atoms. All of them are differen ...

  8. ACM学习历程——HDU3333 Turing Tree(线段树 && 离线操作)

    Problem Description After inventing Turing Tree, 3xian always felt boring when solving problems abou ...

  9. ACM学习历程——POJ3295 Tautology(搜索,二叉树)

    Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some ...

随机推荐

  1. ASIHTTP

    本文转载至 http://www.th7.cn/Program/IOS/201303/128223.shtml     向服务器端上传数据 ASIFormDataRequest ,模拟 Form表单提 ...

  2. 【CodeM初赛B轮】F 期望DP

    [CodeM初赛B轮]F 题目大意:有n个景点,m条无向边,经过每条边的时间需要的时间是li,在第i个景点游览花费的时间是ti,游览完第i个景点可以获得的满意度是hi.你的总时间为k,起初你等概率的选 ...

  3. 爬虫入门【5】PyQuery简介

    PyQuery 目前最新的版本是1.3,基于最新版本进行介绍. 主要根据PyQuery的官方文档进行了更新. from pyquery import PyQuery as pq from lxml i ...

  4. 多语言中的“默认语言”设置

    最近在搞一个多语言的东西,打算如果用户是中文环境就显示中文,其他任何非中文环境就显示英文.换句话说,把默认语言设置成英文. 不过因为VS是中文的,发现即使默认资源文件是英文(AppResource.r ...

  5. iOS OC和JS的交互 javaScriptCore方法封装

    一.javaScriptCore javaScriptCore是一种JavaScript引擎,主要为webKit提供脚本处理能力,javaScriptCore是开源webkit的一部分,他提供了强大的 ...

  6. JS分段传输数据

    <SCRIPT language=javascript> file://数据拆分,并放到相应的hidden域中,在Form的onSubmit事件中激发 function fnPreHand ...

  7. liferay 指定默认首页

    1.登录liferay后,点击控制面板-->设置--> portal设置 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZmVuZ3hpbmdf ...

  8. SAP 物料 移动类型

    [转自 http://blog.sina.com.cn/s/blog_494f9a6b0102edf7.html] SAP 物料 移动类型 (2013-12-03 10:15:01) 转载▼   分类 ...

  9. 关于highcharts-ng

    1.内容都正确但是不显示,使用parseInt()方法转换

  10. || and && 理解

    逻辑或(||): 只要第一个值的布尔值为false,那么永远返回第二个值. 逻辑或属于短路操作,第一个值为true时,不再操作第二个值,且返回第一个值. 逻辑与(&&): 只要第一个值 ...