BC #53 1002 Rikka with Tree

Rikka with Tree

Accepts: 207

Submissions: 815

Time Limit: 2000/1000 MS (Java/Others)

Memory Limit: 65536/65536 K (Java/Others)

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 TT, let F(T,i)F(T,i) be the distance between vertice 1 and vertice ii.(The length of each edge is 1).

Two trees AA and BB are similiar if and only if the have same number of vertices and for each ii meet F(A,i)=F(B,i)F(A,i)=F(B,i).

Two trees AA and BB are different if and only if they have different numbers of vertices or there exist an number ii which vertice ii have different fathers in tree AA and tree BB when vertice 1 is root.

Tree AA is special if and only if there doesn‘t exist an tree BB which AA and BB are different and AA and BB 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 \leq n \leq 1000)n(1≤n≤1000).

Then n-1n−1 lines follow. Each line contains two numbers u,v(1 \leq u,v \leq n)u,v(1≤u,v≤n) , which means there is an edge between uu and vv.

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

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <vector>
 4 #include <algorithm>
 5 using namespace std;
 6 vector <int> edg[1005];
 7 int s[1005],q[1005],n,m;
 8 int d(int x,int y)
 9 {
10     int i,j,k;
11     for(i=0;i<edg[x].size();i++)
12     {
13         int v=edg[x][i];
14         if(s[v]==0)
15         {
16             s[v]=y+1;
17             d(v,y+1);
18             if(y+1>m)
19                 m=y+1;
20         }
21     }
22 }
23 int main()
24 {
25     int i,j,k;
26     while(scanf("%d",&n)!=EOF)
27     {
28         for(i=1;i<=n;i++)
29         {
30             if(edg[i].size())
31             {
32                 edg[i].clear();
33             }
34         }
35         memset(s,0,sizeof(s));
36         memset(q,0,sizeof(q));
37         for(i=1;i<n;i++)
38         {
39             int x,y;
40             scanf("%d %d",&x,&y);
41             edg[x].push_back(y);
42             edg[y].push_back(x);
43         }
44         s[1]=1;m=0;
45         d(1,1);
46         for(i=1;i<=n;i++)
47             q[s[i]]++;
48         int flg=0;
49         for(i=1;i<m;i++)
50         {
51             if(q[i]>1)
52             {
53                 flg=1;
54                 break;
55             }
56         }
57         if(flg==1)
58             printf("NO\n");
59         else
60             printf("YES\n");
61     }
62     return 0;
63 }

时间: 2024-11-05 02:17:13

BC #53 1002 Rikka with Tree的相关文章

BC#53 1001 Rikka with Graph

Rikka with Graph Accepts: 353 Submissions: 1174 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math

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】4908 (杭电 BC #3 1002题)BestCoder Sequence ——哈希

BestCoder Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 573    Accepted Submission(s): 201 Problem Description Mr Potato is a coder.Mr Potato is the BestCoder. One night, an amazing s

hdu 5365 (bc #50 1002 )Run

Run Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 594    Accepted Submission(s): 265 Problem Description AFA is a girl who like runing.Today,he download an app about runing .The app can record

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,

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 4908 (杭电 BC #3 1002题)BestCoder Sequence(DP)

题目地址:HDU 4908 这个题是从m开始,分别往前DP和往后DP,如果比m大,就比前面+1,反之-1.这样的话,为0的点就可以与m这个数匹配成一个子串,然后左边和右边的相反数的也可以互相匹配成一个子串,然后互相的乘积最后再加上就行了.因为加入最终两边的互相匹配了,那就说明左右两边一定是偶数个,加上m就一定是奇数个,这奇数个的问题就不用担心了. 代码如下: #include <iostream> #include <stdio.h> #include <string.h&g

hdu 5425 Rikka with Tree II(暴力)

题目链接:hdu 5425 Rikka with Tree II 直接枚举就好了,当概率极小时贡献值可以忽略. #include <cstdio> #include <cstring> #include <cmath> #include <vector> #include <queue> #include <algorithm> using namespace std; const int maxn = 1e5 + 5; int N,

【BC#24 1002 HDOJ 5273】Dylans loves sequence

[BC#24 1002 HDOJ 5273]Dylans loves sequence 逆序对动归 比赛时候各种奇葩姿势都上了个遍 归并也憋出来了 谁知道就给我看这个.... 赛后有的思路 收到赛后题解的启发 dp[l][r]是l~r之间逆序对 先暴力把dp[1][1~n]枚举出来 然后i从2~n枚举左边界 右边界从i+1~n 这样dp[i][j] 即求出了区间左端点从2往后的所有逆序对数 而dp[i][j]即为dp[i-1][j]中吧i-1的逆序对数减去的余数 这样顺序暴力即可......还是