hdu54235423 Rikka with Tree

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 TT, let F(T,i)F(T,i) be the distance between vertice 1 and vertice ii.(The length of each edge is 1).

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

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

Tree AA is special if and only if there doesn't exist an tree BB which AA and BB are different and AA and BB 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 \leq n \leq 1000)n(1n1000).

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

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

你这都第几次看错题了?!能不能认真点?!上点心!!题意说

特殊的当且仅当不存在一棵和它不同的树和它相似等价于形如一条龙,除了最后一层可以有多个节点,其它层只能有一个节点

反正你读错题了->_->读对了也想不到这么解,也就这水平了 呵呵 怪不得rating做一场低好几十微笑

#include <iostream>
#include <stdio.h>
#include <string.h>
#include<set>
#include<vector>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    while(cin>>n){
       typedef set<int>b;
        b a[1005];
       // std::set<std::a<int,1000>>
        int u,v,nn=n;
        bool flag=1;
        for(int i=0;i<n-1;i++){
            cin>>u>>v;
            a[u].insert(v);
        }
        for(int i=1;i<1002;i++){
            if(a[i].size()>1){
                for(set<int>::iterator j=a[i].begin();j!=a[i].end();++j){
                    int t=*j;
                    int d=a[t].size();
                    if(d!=0){flag=0;break;}
                }

            }
        }
        if(flag)cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}


你可能感兴趣的:(算法,HDU,BestCoder,思维题)