hdu 5423 Rikka with Tree(dfs)

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 Aand 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

Source

BestCoder Round #53 (div.2)

根据题意可以构造出来的特殊树有三种情况(全是以结点1为根节点),,第一种是一条直线,第二种是一条直线末尾开花,第三种是直接开花。如图所示。然后就可以知道对于每一个深度的点数为1,1,1,...,x,0,0,...。然后dfs一遍找一下就可以了。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<vector>
 5 #include<set>
 6 #include<map>
 7 #include<queue>
 8 #include<algorithm>
 9 using namespace std;
10 #define N 1006
11 vector<int>g[N];
12 vector<int>deep[N];
13 int vis[N];
14 void dfs(int st,int d){
15     vis[st]=1;
16     deep[d].push_back(st);
17     for(int i=0;i<g[st].size();i++){
18         int u=g[st][i];
19         if(!vis[u]){
20             dfs(u,d+1);
21         }
22     }
23 }
24 int main()
25 {
26     int n;
27     while(scanf("%d",&n)==1){
28
29         for(int i=0;i<=n;i++) {
30             g[i].clear();
31             deep[i].clear();
32         }
33         memset(vis,0,sizeof(vis));
34
35         for(int i=1;i<n;i++){
36             int u,v;
37             scanf("%d%d",&u,&v);
38             g[u].push_back(v);
39             g[v].push_back(u);
40         }
41         dfs(1,0);
42
43
44         int t=0;
45         while(deep[t].size()==1) t++;
46
47         int num=deep[t].size();
48         if(num+t!=n) {
49             printf("NO\n");
50         }else{
51             printf("YES\n");
52         }
53
54     }
55     return 0;
56 }

时间: 2024-10-11 17:07:43

hdu 5423 Rikka with Tree(dfs)的相关文章

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

题意: 输入一棵树,判断这棵树在以节点1为根节点时,是否是一棵特殊的树. 相关定义: 1.  定义f[A, i]为树A上节点i到节点1的距离,父节点与子节点之间的距离为1. 2.  对于树A与树B,如果A与B的节点数相同,且无论i为何值,f[A, i]与f[B, i]都相等,则A与B为两棵相似的树. 3.  对于一棵树A,在以节点1为根节点的情况下,如果不存在与其它树与A相似,则A是一棵特殊的树. 输入: 包含多组输入样例. 每组输入样例中首先输入一个整数n,表示一棵含有n个节点的树. 接下来n

HUD5423 Rikka with Tree(DFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5423 Rikka with Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 321    Accepted Submission(s): 162 Problem Description As we know, Rikka is p

hdu5423 Rikka with Tree(DFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5423 题目大意:BC round# 53上有中文题意= = 思路:画图可以发现,其实题目本身不难.因为他有n个点,n条边,所以可以确定只有一棵树. 对于树中的某一个点来说,如果他有多个分支,然后分支下面又有边连出去,那么这种情况就一定是NO了. 比如1和2.3相连,然后2又与4相连,3与5相连,那么4和5就可以调换位置,此时这颗树就不是特殊的了. 所以我可以利用深搜记录下所有点的深度,对深度排序以后

HDU - 5202 - Rikka with string (DFS)

Rikka with string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 214    Accepted Submission(s): 109 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation

hdu 1016 Prime Ring Problem (dfs)

一切见注释. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; bool vis[22]; int n; int ans[22]; int top; bool isprime(int x)//判断素数 { for(int i=2;i<x;i++) if(x%i==0)return false; return

HDU 2209 翻纸牌游戏(dfs)

翻纸牌游戏 Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2180    Accepted Submission(s): 787 Problem Description 有一种纸牌游戏,很有意思,给你N张纸牌,一字排开,纸牌有正反两面,开始的纸牌可能是一种乱的状态(有些朝正,有些朝反),现在你需要整理这些纸牌.但是麻烦的是,每当你翻一张

HDU 5423:Rikka with Tree Dijkstra算法

Rikka with Tree Accepts: 207 Submissions: 815 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的: 对于一棵树TT,令F(T,i)F(T,i)为点1到点ii的最短距离(边长是1). 两棵树AA和BB是相似的当且仅当他们顶点数相同且对于任意的ii都有

hdu 5423 Rikka with Tree 树的性质

Rikka with Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 165    Accepted Submission(s): 85 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s

HDU 5423 Rikka with Tree(水题)

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,