zoukankan      html  css  js  c++  java
  • 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(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
    ");
    59         else
    60             printf("YES
    ");
    61     }
    62     return 0;
    63 }
    View Code
  • 相关阅读:
    优酷土豆的Redis服务平台化之路
    [C#]使用Gembox.SpreadSheet向Excel写入数据及图表
    [C#]使用Join与GroupJoin将两个集合进行关联与分组
    [C#]使用TcpListener及TcpClient开发一个简单的Chat工具
    超棒黑客必备清单
    [C#]使用dnSpy对目标程序(EXE或DLL)进行反编译修改并编译运行
    [C#源代码]使用SCPI指令对通信端口(RS232/USB/GPIB/LAN)进行仪器编程
    [C#]使用Costura.Fody将源DLL合并到目标EXE
    [C#]使用ILMerge将源DLL合并到目标EXE(.NET4.6.2)
    详解C#7.0新特性
  • 原文地址:https://www.cnblogs.com/cyd308/p/4770793.html
Copyright © 2011-2022 走看看