• 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
  • 相关阅读:
    面试题_回溯法
    面试题专题_目录
    面试题_有序数组的二分法解法
    Linux 信号捕获堆栈信息
    Git 简单基础命令的应用
    【转】【C#】常用ToString()方法总结
    【转】【Revit】Revit 二次开发 特殊族实例参照获取
    使用移除图片背景(提取人像)的开源项目rembg
    X64Dbg 介绍>表达式
    0314遗忘的角落,一文说清什么是测试策略
  • 原文地址:https://www.cnblogs.com/cyd308/p/4770793.html
Copyright © 2020-2023  润新知