题目链接:
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[N];
int sum[N];
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;
}