zoukankan      html  css  js  c++  java
  • hdu---5423---Rikka with Tree

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5423

    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 , let  be the distance between vertice 1 and vertice .(The length of each edge is 1). 
    Two trees  and  are similiar if and only if the have same number of vertices and for each  meet . 
    Two trees  and  are different if and only if they have different numbers of vertices or there exist an number  which vertice  have different fathers in tree  and tree  when vertice 1 is root. 
    Tree  is special if and only if there doesn't exist an tree  which  and  are different and  and  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 . 
    Then  lines follow. Each line contains two numbers  , which means there is an edge between  and .

    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
    问题描述
    众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的:
    
    对于一棵树TT,令F(T,i)F(T,i)为点1到点ii的最短距离(边长是1). 
    
    两棵树AA和BB是相似的当且仅当他们顶点数相同且对于任意的ii都有F(A,i)=F(B,i)F(A,i)=F(B,i).
    
    两棵树AA和BB是不同的当且仅当他们定点数不同或者存在一个ii使得以1号点为根的时候ii在两棵树中的父亲不同。
    
    一棵树AA是特殊的当且仅当不存在一棵和它不同的树和它相似。
    
    现在勇太想知道一棵树到底是不是特殊的。
    
    当然,这个问题对于萌萌哒六花来说实在是太难了,你可以帮帮她吗?
    输入描述
    数据组数不超过100组。每组数据的第一行一个整数n(2 leq n leq 1000)n(2n1000)。
    
    接下来n-1n1行。每行两个整数u,v(1 leq u,v leq n)u,v(1u,vn),代表给定树上的一条边。
    输出描述
    对于每一组数据,如果给定树是特殊的输出"YES"否则输出"NO"。
     Mean:
    判断是否是一棵特殊树。
    特殊树的定义如中文翻译
    analyse:
    eample1:
    3
    1 2   1----2----3
    2 3
    F(A,1)=0;
    F(A,2)=1;
    F(A,3)=2;
    变化成另一棵树的话(但并不相似):
    3
    1 3  1----3----2
    2 3
    F(B,1)=0;
    F(B,2)=2;
    A(B,3)=1;
    无论怎样变化都做不到F(A,i)==F(B,i)相同。
    eample2
    4
    1 2        1---2---3
    2 3        |
    1 4        4
    F(A,1)=0;
    F(A,2)=1;
    F(A,3)=2;
    F(A,4)=3;
    4
    1 2               1---4---3          
    1 4      |
    3 4      2
    F(B,1)=0;
    F(B,2)=1;
    F(B,3)=2;
    F(B,4)=3;
    容易发现一棵树是特殊的,当且仅当非叶子的结点个数不多于1个。
    那么DFS一遍求出每一层结点个数判断即可。
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <cmath>
    #include<vector>
    #include<queue>
    #include<algorithm>
    
    using namespace std;
    typedef long long LL;
    
    const int maxn=1009;
    const int INF=0x3f3f3f3f;
    const int mod=2009;
    
    int maps[maxn][maxn];
    int dist[maxn];
    int cnt[maxn];
    int n;
    
    void DFS(int u, int d)
    {
        dist[u]=d;
        for(int i=1; i<=n; i++)
        {
            if(maps[u][i]&&dist[i]==-1)
                DFS(i, d+1);
        }
    }
    int main()
    {
        while(~scanf("%d", &n))
        {
            int u, v;
            memset(maps, 0, sizeof(maps));
            for(int i=1; i<n; i++)
            {
                scanf("%d %d", &u, &v);
                maps[u][v]=maps[v][u]=1;
            }
    
            memset(dist, -1, sizeof(dist));
            memset(cnt, 0, sizeof(cnt));
    
            DFS(1,0);
            for(int i=1; i<=n; i++)
                cnt[dist[i]]++;
            int index=0, j;
            for(j=1; j<n; j++)
            {
                if(cnt[j])
                {
                    if(cnt[j]<=cnt[index]&&cnt[index]>1)
                        break;
                    else
                        index=j;
                }
            }
    
            if(j==n)
                puts("YES");
            else
                puts("NO");
        }
        return 0;
    }
  • 相关阅读:
    ftpserver / FTP model:PORT/PASV/EPRT/EPSV
    windows 环境下搭建electricSearch+kibana
    springBoot2.x整合 logback实现日志记录
    springBoot使用aop添加处理rest请求 打印请求时间 和请求参数
    springCloud 使用feign复制请求头调用其他服务 content-length不一致导致调用失败
    mysql查询重复用户最新的一条数据
    【开源】 开源社区
    【数据库】 SQL 使用注意点
    【数据库】 SQL 常用语句
    【数据结构】 List 简单实现
  • 原文地址:https://www.cnblogs.com/w-y-1/p/5798526.html
Copyright © 2011-2022 走看看