码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5423 Rikka with Tree 树的性质

时间:2015-08-30 01:05:52      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:

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 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 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 numberi技术分享 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?
 

Input
There are no more than 100 testcases.

For each testcase, the first line contains a number n(1n1000)技术分享.

Then n?1技术分享 lines follow. Each line contains two numbers u,v(1u,vn)技术分享 , which means there is an edge between u技术分享 and v技术分享.
 

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
 

Source
 

Recommend
hujie   |   We have carefully selected several similar problems for you:  5426 5425 5422 5421 5420 
题意,给出一个树,判定是否存在相似的不同的树,相似定义为,两个树的每个结点到其1结点的距离相同,相同定义为,两个树的每个结点到其结点的父结点相同(1做为根)

只要,每层结点数是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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu 5423 Rikka with Tree 树的性质

标签:

原文地址:http://blog.csdn.net/mengzhengnan/article/details/48094099

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!