3 1 2 2 3 4 1 2 2 3 1 4
YES NOHintFor the second testcase, this tree is similiar with the given tree: 4 1 2 1 4 3 4
只要,每层结点数是1 1 1 .... x的结构的树,就一定是唯一的。
#define N 1005
#define M 100005
#define maxn 205
#define MOD 1000000000000000007
int n,a,b,num[N],depth;
vector<int> p[N];
queue<int> q;
bool vis[N];
void DFS(int top,int step){
num[step]++;
vis[top] = true;
depth = max(depth,step);
FI(p[top].size()){
int goal = p[top][i];
if(!vis[goal]){
DFS(goal,step+1);
}
}
}
bool BFS(){
fill(vis,false);
fill(num,0);
depth = 0;
DFS(1,0);
FI(depth+1){
if(num[i] >= 2 && i != depth) return false;
}
return true;
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
while(S(n)!=EOF)
{
FI(n+1) p[i].clear();
FI(n-1){
S2(a,b);
p[a].push_back(b);
p[b].push_back(a);
}
if(BFS())
printf("YES\n");
else
printf("NO\n");
}
//fclose(stdin);
//fclose(stdout);
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/mengzhengnan/article/details/48094099