HDU5423 Rikka with Tree(树,深搜DFS)

题目:

Rikka with Tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 883    Accepted Submission(s): 403


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 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?
 

Input
There are no more than 100 testcases. 

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

Then  n1  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:   6022  6021  6020  6019  6018 
 

Statistic |  Submit |  Discuss |  Note
思路:

这个题做法很简单,但是题意很难懂,我在这里描述一下。

题目中定义了三种情况

1.相似的树:如果两棵树有相同的节点数量,并且每个节点到根节点的距离相等

2.不同的树:两棵树的节点数量不同,或者当1为根节点时存在一个节点它的节点的父亲节点在第一棵树和第二棵树中不一样

3.特殊的树:不存在一棵树使他们既相似又不同

题目给你了一棵树问他是不是特殊的树。

其实也就是找一颗既相似又不同的树,找到了就是输出NO,没找到就输出YES

经过分析,我们要求得到特殊的树,那么要满足的条件是:

一棵树只有一个节点有多个儿子且这个树不能有孙子


我们只要用搜索判断一下它如果有一个节点有多个儿子且还有孙子,那么肯定不是特殊的树,问题就解决了

代码:

#include <cstdio>
#include <cstring>
#include <cctype>
#include <string>
#include <set>
#include <iostream>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define mod 10007
#define N 1000+20
#define M 1000000+10
#define ll long long
using namespace std;
vector<int>q[N];
int flag=0;
void dfs(int x,int op)
{
    if(flag||!q[x].size())
        return;
    if(op&&q[x].size()>0)
    {
        flag=1;
        return;
    }
    if(q[x].size()>1)//当儿子数大于1时
    {
        for(int i=0; i<q[x].size(); i++)
        {
            dfs(q[x][i],1);
        }
    }
    else
        dfs(q[x][0],0);
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        flag=0;
        for(int i=1; i<=n; i++)
            q[i].clear();
        int u,v;
        for(int i=0; i<n-1; i++)
        {
            scanf("%d%d",&u,&v);
            q[u].push_back(v);
        }
        dfs(1,0);
        if(flag)
            puts("NO");
        else
            puts("YES");
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值