题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5423

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 and vertice i.(The length of each edge is ). 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 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 testcases. For each testcase, the first line contains a number n(≤n≤). Then n− lines follow. Each line contains two numbers u,v(≤u,v≤n) , 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 Sample Output
YES
NO

题意:两棵树中所有点到根节点的距离都相同时这两棵树叫做相似的树,两棵树中有某个节点的父节点不同时这两棵树叫不同的数,如果一棵树不存在另一棵树与之不同且相似则称这棵树为特殊的树,给出一棵树判断是否为特殊的树

方法:先分层,在判断是一条直树,还是扫帚型的

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <math.h>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
#define met(a,b) memset(a,b,sizeof(a));
#define N 1034
int Map[N][N];
int con[N];
int n;
void bfs(int s,int m)
{
con[s]=m;
for(int i=;i<=n;i++)
{
if(Map[s][i]&&!con[i])
{
bfs(i,m+);
}
}
}
int main()
{
int a,b;
int sum[N];
while(scanf("%d",&n)!=EOF)
{
met(Map,);met(con,);
for(int i=;i<n;i++)
{
scanf("%d %d",&a,&b);
Map[a][b]=Map[b][a]=;
}
bfs(,);///对树分层
met(sum,);int ans=;
for(int i=;i<=n;i++)
{
sum[con[i]]++;
}
for(int i=;i<=n;i++)
{
if(sum[i]==)///是否是一条线到底
ans++;
else
{
if(sum[i+]==)///是否是扫帚型
ans=n;
break;
}
}
if(ans==n)
printf("YES\n");
else
printf("NO\n");
}
return ;
}

最新文章

  1. Congruence relation 同余关系
  2. Python 命名空间
  3. [转]C#创建Windows服务与安装
  4. Javascript基础系列之(六)循环语句(do while循环)
  5. 【BZOJ】2049: [Sdoi2008]Cave 洞穴勘测(lct/并查集)
  6. YouTube技术架构
  7. python制作安装包(setup.py)
  8. Content-Disposition的使用和注意事项(转载)
  9. java导入excel
  10. js 图片转换为base64
  11. [转帖]5G网速那么快,基站辐射会很大吗?
  12. Unity实现c#热更新方案探究(一)
  13. 前端 CSS预处理器Less
  14. NOIP2013 花匠解题报告
  15. windows模糊查询指定进程是否存在
  16. JTopo使用心得
  17. LABVIEW串口通信基础
  18. JSPatch实现原理详解
  19. lamp docker apache2 supervisor monitor
  20. HTMLCanvasElement.toBlob() 兼容性及使用

热门文章

  1. HDU 4588 Count The Carries 数学
  2. Cocos2d-x学习笔记(10)(CCMenu菜单)
  3. POJ 2406 Power Strings KMP运用题解
  4. 让java程序在后台一直执行(例如putty关闭后后台程序继续运行)
  5. 更新mac自带的python
  6. 设置EXCEL2010打开多个独立窗口
  7. UNIX标准化及实现之限制
  8. Cloudera集群中提交Spark任务出现java.lang.NoSuchMethodError: org.apache.hadoop.hbase.HTableDescriptor.addFamily错误解决
  9. 用Linux安装光盘修复GRUB
  10. 前端必会的js知识总结整理