Rikka with Tree(DFS+树的性质)

173 篇文章 3 订阅
171 篇文章 0 订阅

Link:http://acm.hdu.edu.cn/showproblem.php?pid=5423

Rikka with Tree

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


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
 

中文题意:

Rikka with Tree

 
 Accepts: 207
 
 Submissions: 815
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 65536/65536 K (Java/Others)
问题描述
众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的:

对于一棵树TT,令F(T,i)F(T,i)为点1到点ii的最短距离(边长是1). 

两棵树AABB是相似的当且仅当他们顶点数相同且对于任意的ii都有F(A,i)=F(B,i)F(A,i)=F(B,i).

两棵树AABB是不同的当且仅当他们定点数不同或者存在一个ii使得以1号点为根的时候ii在两棵树中的父亲不同。

一棵树AA是特殊的当且仅当不存在一棵和它不同的树和它相似。

现在勇太想知道一棵树到底是不是特殊的。

当然,这个问题对于萌萌哒六花来说实在是太难了,你可以帮帮她吗?
输入描述
数据组数不超过100组。每组数据的第一行一个整数n(2 \leq n \leq 1000)n(2n1000)。

接下来n-1n1行。每行两个整数u,v(1 \leq u,v \leq n)u,v(1u,vn),代表给定树上的一条边。
输出描述
对于每一组数据,如果给定树是特殊的输出"YES"否则输出"NO"。
输入样例
3
1 2
2 3
4
1 2
2 3
1 4
输出样例
YES
NO
Hint
对于第二组数据下面这棵树和它相似。
4
1 2
1 4
3 4



官方题解:

Rikka with Tree

显然一棵树是独特的当且仅当任意处于每一个深度的点数是"1 1 1 1 ... 1 1 x"。所以直接DFS一下求出每一个点到根的距离然后判断一下就好了 。

AC code:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define N 50010
#define M 10010
#define INF 1000000000
int n;
int  dis[N];
bool leaf[N],vis[N];
vector<int>vec[N],link[N];
int dep;
void dfs(int u,int depth)
{
	dis[u]=depth;//记录每个点与根节点的距离(即深搜的深度) 
	vec[depth].push_back(u);
	vis[u]=true;
	if(u!=1&&link[u].size()==1)
	{
		leaf[u]=true;
		dep=max(dep,depth);
		return;
	}
	for(int i=0;i<link[u].size();i++)
	{
		if(!vis[link[u][i]])
		{
			dfs(link[u][i],depth+1);
			vis[u]=false;
		}
	}
}
bool judge()
{
	for(int i=1;i<dep;i++)//在小于深搜树的高度时枚举深搜树的每一层结点数,若中间某一层结点数大于1,则树是相似的 
	{
		/*for(int j=0;j<vec[i].size();j++)
		{
			if(!leaf[vec[i][j]]&&vec[i].size()>1)//等价于若某一层的结点不是叶子结点且该层结点数大于1,则树是相似的 
				return false;
		}*/
		if(vec[i].size()>1)
			return false;
	}
	return true;
}
int main(){
   //freopen("in.txt","r",stdin);
   int i,u,v;
   while(scanf("%d",&n)!=EOF)
   {
		for(i=0;i<=n;i++)
		{
			vec[i].clear();
			vis[i]=false;
			leaf[i]=false;
			link[i].clear();
		}
   		for(i=1;i<=n-1;i++)
   		{
   			scanf("%d%d",&u,&v);
			link[u].push_back(v);
			link[v].push_back(u);	
		}
		dep=0;
		dfs(1,0);
		if(judge())
		{
			printf("YES\n");
		}
		else
		{
			printf("NO\n");
		}
   }
   return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值