您的位置:首页 > 其它

hdu 5423 Rikka with Tree(深搜)

2015-08-31 09:38 267 查看
题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5423

解题思路:

看懂了题目,就是水题,看不懂,无从下手,就是难题。。。

显然一棵树是独特的当且仅当任意处于每一个深度的点数是"1 1 1 1 ... 1 1 x"。所以直接DFS一下求出每一个点到根的距离然后判断一下就好了 。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

const int N = 1005;
vector<int> v
;
int sum
;
int cnt;//深度

void dfs(int u,int fa,int dep){
    int l = v[u].size();
    for(int i = 0; i < l; i++){
        int x = v[u][i];
        if(x == fa)
            continue;
        dfs(x,u,dep+1);
    }
    sum[dep]++;
    cnt = max(cnt,dep);
}

bool check(){
    for(int i = 1; i < cnt; i++)
        if(sum[i] > 1)
            return false;
    return true;
}

int main(){
    int n;
    while(~scanf("%d",&n)){
        int x,y;
        for(int i = 1; i<=n; i++)
            v[i].clear();
        for (int i = 1; i<n; i++){
            scanf("%d%d",&x,&y);
            v[x].push_back(y);
            v[y].push_back(x);
        }
        memset(sum,0,sizeof(sum));
        cnt = 0;
        dfs(1, 1, 1);
        if(check())
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: