HDU 5432 Rikka with Tree (BestCoder Round #53 (div.2))

题目大意:给你一个树 判断这棵树是否是独特的

一颗树是独特的条件:不存在一颗和它本身不同但相似的树

两颗树相似条件:两颗树中点的数量相等且相对应的点的深度相同

如第2个样例

4

1 2

2 3

1 4

4

1 2

1 4

3 4

如图:这两棵树的点的数量相等且相应的点的深度deep相同,所以这两棵树相似,所以样例2存在一颗树与它不同但相似,即不特殊

运用广搜统计每个点的深度

要想一颗树特殊,只有保证他的分支下面不再有子节点

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 1010

using namespace std;

struct Edge
{
    int u, v, cnt, next;
}edge[N * N];

int head[N], j, cnt;
int a[N];//记录各个点的深度
bool vis[N];

void Add(int u, int v)//邻接表
{
    edge[cnt].u = u;
    edge[cnt].v = v;
    edge[cnt].next = head[u];
    head[u] = cnt++;
}

void Init()
{
    memset(head, -1, sizeof(head));
    memset(a, 0, sizeof(a));
    memset(vis, false, sizeof(vis));
    cnt = j = 0;
}

void DFS(int u, int deep)
{
    int v, i;
    vis[u] = true;
    for(i = head[u]; i != -1 ; i = edge[i].next)
    {
        v = edge[i].v;
        if(!vis[v])
        {
            vis[v] = true;
            DFS(v, deep + 1);
        }
    }
    a[j++] = deep;
    return ;
}//查找记录各个点的深度

int cmp(const void *a, const void *b)
{
    return *(int *)a - *(int *)b;
}

int main()
{
    int n, u, v;
    while(~scanf("%d", &n))
    {
        Init();
        for(int i = 1 ; i < n ; i++)
        {
            scanf("%d%d", &u, &v);
            Add(u, v);
            Add(v, u);
        }
        DFS(1, 0);
        qsort(a, j, sizeof(a[0]), cmp);
        int deep = 0, f = 0, fl = 0;
        for(int i = 1 ; i < j ; i++)
        {
            if(a[i - 1] == a[i])
            {
                fl = 1;//如果两个点的深度相同,就存在分支,即标记有分支
                deep = a[i];
            }
            if(fl == 1 && a[i] > deep)
                f = 1;//如果存在分支且分支的下面还有节点则输出NO
        }
        if(f == 0)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

时间: 2024-10-11 11:11:36

HDU 5432 Rikka with Tree (BestCoder Round #53 (div.2))的相关文章

hdu 5424 Rikka with Graph II (BestCoder Round #53 (div.2))(哈密顿通路判断)

http://acm.hdu.edu.cn/showproblem.php?pid=5424 哈密顿通路:联通的图,访问每个顶点的路径且只访问一次 n个点n条边 n个顶点有n - 1条边,最后一条边的连接情况: (1)自环(这里不需要考虑): (2)最后一条边将首和尾连接,这样每个点的度都为2: (3)最后一条边将首和除尾之外的点连接或将尾和出尾之外的点连接,这样相应的首或尾的度最小,度为1: (4)最后一条边将首和尾除外的两个点连接,这样就有两个点的度最小,度都为1 如果所给的图是联通的话,那

哈密顿图 BestCoder Round #53 (div.2) 1003 Rikka with Graph II

题目传送门 题意:判断是否为哈密顿图 分析:首先一种情况是不合法的:也就是度数为1的点超过2个:合法的有:,那么从度数为1的点开始深搜,如果存在一种走法能够走完n个点那么存在哈密顿路 收获:学习资料 代码: /************************************************ * Author :Running_Time * Created Time :2015-8-29 20:37:34 * File Name :C.cpp *******************

HDU 5056 Boring count(BestCoder Round #11 (Div. 2))

Problem Description: You are given a string S consisting of lowercase letters, and your task is counting the number of substring that the number of each lowercase letter in the substring is no more than K. Input: In the first line there is an integer

BestCoder Round #53 (div.1)

Problem A: 题目大意: 给出以节点1为根的一棵树A,判断它是否是特殊的.一棵树是特殊的当且仅当不存在和它不完全相同的一棵树B,使得A中点i到点1的距离和B中相等. 题解: 假设一个点x的深度是d,它的父亲是y,如果存在一个深度为d-1的点z,那么把x从y下面移到z下面就可以得到树B了.所以求出每个深度的点的个数,只有当所有深度的点的个数都为1,最大深度的点的个数任意的时候 树是特殊的. Problem B: 题目大意: 给出N个点N条边的无向图判断是否存在哈密顿路. N<=1000.

hdu 5423 Rikka with Tree(dfs)

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 T, let F(T,i) be the distance between vertice 1 and vertice i.(The length of

hdu 5423 Rikka with Tree 树的性质

Rikka with Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 165    Accepted Submission(s): 85 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s

HDU 5423 Rikka with Tree(水题)

Rikka with Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 292    Accepted Submission(s): 149 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation,

BestCoder Round #53

现在博客更新比较少了,就当我还活着吧 Rikka with Graph 题目传送:HDU - 5422 - Rikka with Graph AC代码: #include <map> #include <set> #include <list> #include <cmath> #include <deque> #include <queue> #include <stack> #include <bitset>

hdu 5425 Rikka with Tree II(暴力)

题目链接:hdu 5425 Rikka with Tree II 直接枚举就好了,当概率极小时贡献值可以忽略. #include <cstdio> #include <cstring> #include <cmath> #include <vector> #include <queue> #include <algorithm> using namespace std; const int maxn = 1e5 + 5; int N,