HDU 5423 Rikka with Tree(DFS)

题意:略

解题思路:首先若保证一棵树是一颗特殊的树,其深度的分布应该是1 1 1 1 ……X 除了最后的叶子节点,其余位置不允许有分叉开花。(一开始错了几次,没考虑最深的一层可以分叉)。深搜记录节点深度。再乱搞一下就行了。使用并查集记录节点深度也可以(没尝试)。


Rikka with Tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 482    Accepted Submission(s): 230


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 each edge is 1).

Two trees A and B are similiar if and only if the have same number of vertices and for each i meet F(A,i)=F(B,i) .

Two trees A and B are different if and only if they have different numbers of vertices or there exist an number i which vertice i have different fathers in tree A and tree B when vertice 1 is root.

Tree A is special if and only if there doesn't exist an tree B which A and B are different and A and B 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(1n1000) .

Then n1 lines follow. Each line contains two numbers u,v(1u,vn) , which means there is an edge between u and v .
 

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


#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cctype>
#include<list>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>

using namespace std;

#define FOR(i, s, t) for(int i = (s) ; i <= (t) ; ++i)
#define REP(i, n) for(int i = 0 ; i < (n) ; ++i)

int buf[10];
inline long long read()
{
    long long x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}

inline void writenum(int i)
{
    int p = 0;
    if(i == 0) p++;
    else while(i)
        {
            buf[p++] = i % 10;
            i /= 10;
        }
    for(int j = p - 1 ; j >= 0 ; --j) putchar('0' + buf[j]);
}
/**************************************************************/
#define MAX_N 1005
const int INF = 0x3f3f3f3f;
int out[MAX_N];
struct node
{
    int to, next;
};
int head[MAX_N];

node edge[MAX_N << 2];
int top = 0;
int vis[MAX_N];
int cnt[MAX_N];
int iq = 0;
inline void init()
{
    memset(head, -1, sizeof(head));
    memset(vis, 0, sizeof(vis));
    memset(cnt, 0, sizeof(cnt));
    iq = 0;
    top = 0;
}

void add_edge(int u, int v)
{
    edge[top].to = v;
    edge[top].next = head[u];
    head[u] = top++;
}

void dfs(int u, int deep)
{
    vis[u] = 1;
    for(int k = head[u] ; k != -1 ; k = edge[k].next)
    {
        int v = edge[k].to;
        if(!vis[v])
        {
            dfs(v, deep + 1);
        }
    }
    cnt[iq++] = deep;
}



int main()
{
    int n;
    while(~scanf("%d", &n))
    {
        init();
        for(int i = 1 ; i < n ; i++)
        {
            int u = read();
            int v = read();
            add_edge(u, v);
            add_edge(v, u);
        }
        dfs(1, 0);
        iq--;
        sort(cnt, cnt + iq);
        int tmp;
        int flag = 0;
        int ok = 0;
        for(int i = 1 ; i < iq ; i++)
        {
            if(cnt[i] == cnt[i -1])
            {
                flag = 1;
                tmp = cnt[i];
            }
            if(flag && cnt[i] > tmp)
            {
                ok = 1;
                break;
            }
        }
        if(ok) cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值