Nature of the hdu 5423 Rikka with Tree, hdu5423

Source: Internet
Author: User

Nature of the hdu 5423 Rikka with Tree, hdu5423

Rikka with Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission (s): 165 Accepted Submission (s): 85


Problem DescriptionAs 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?
InputThere 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 U And V .
OutputFor each testcase, if the tree is special print "YES", otherwise print "NO ".
Sample Input
31 22 341 22 31 4
 
Sample Output
YESNOHintFor the second testcase, this tree is similiar with the given tree:41 21 43 4 
 
SourceBestCoder Round #53 (div.2)
Recommendhujie | We have carefully selected several similar problems for you: 5426 5425 5422 5421 5420 question, give a tree, determine whether there are similar different trees, similar definition is, each node of the two trees has the same distance to its 1 node. The same definition is that each node of the two trees has the same parent node to its node (1 as the root node)

As long as the number of knots at each layer is a tree in the structure of 1 1... x, it must be unique.

#define N 1005#define M 100005#define maxn 205#define MOD 1000000000000000007int 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;}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.