您的位置:首页 > 其它

HDU 5423 Rikka with Tree(DFS)

2015-08-31 22:56 423 查看
题意:略

解题思路:首先若保证一棵树是一颗特殊的树,其深度的分布应该是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(1≤n≤1000).

Then n−1
lines follow. Each line contains two numbers u,v(1≤u,v≤n)
, 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

HintFor 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: