BC #53 1002 Rikka with Tree

本文介绍了一个关于树结构的数学问题——RikkawithTree,并提供了详细的解析及算法实现。该问题要求判断一棵树是否为特殊树,即是否存在另一棵树与其相似但不同。通过递归计算树中各节点到根节点的距离并分析这些距离的分布来解决此问题。

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(1n1000).

Then n-1n1 lines follow. Each line contains two numbers u,v(1 \leq u,v \leq n)u,v(1u,vn) , 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 }
View Code

 

转载于:https://www.cnblogs.com/cyd308/p/4770793.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值