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
  • 相关阅读:
    Java Swing TextArea 滚动条和获得焦点
    Windows下一个AndroidStudio 正在使用Git(AndroidStudio工程GitHub关联)
    我们将与操作系统工作谈一场无私的爱──《云情人》思考
    CSDN markdown 编辑 三 基本语法
    Android项目包装apk和apk反编译,xml反编译
    char (*(*p[3])( int ))[5] 等等一系列 左右法则
    typedef 优于 #define
    int *(*a[5])(int, char*)
    C++宏定义详解
    STL 案例分析
  • 原文地址:https://www.cnblogs.com/cyd308/p/4770793.html
Copyright © 2011-2022 走看看