Problem Description
  1. 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:
  2.  
  3. For a tree T, let F(T,i) be the distance between vertice and vertice i.(The length of each edge is ).
  4.  
  5. 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).
  6.  
  7. 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 Aand tree B when vertice is root.
  8.  
  9. 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.
  10.  
  11. Now he wants to know if a tree is special.
  12.  
  13. It is too difficult for Rikka. Can you help her?
 
Input
  1. There are no more than testcases.
  2.  
  3. For each testcase, the first line contains a number n(≤n≤).
  4.  
  5. Then n lines follow. Each line contains two numbers u,v(≤u,vn) , which means there is an edge between u and v.
 
Output
  1. For each testcase, if the tree is special print "YES" , otherwise print "NO".
 
Sample Input
  1.  
 
Sample Output
  1. YES
  2. NO
Hint

For the second testcase, this tree is similiar with the given tree:

                                                                                       4
                                                                                       1 2
                                                                                       1 4
                                                                                       3 4
 
Source
 

根据题意可以构造出来的特殊树有三种情况(全是以结点1为根节点),,第一种是一条直线,第二种是一条直线末尾开花,第三种是直接开花。如图所示。然后就可以知道对于每一个深度的点数为1,1,1,...,x,0,0,...。然后dfs一遍找一下就可以了。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<vector>
  5. #include<set>
  6. #include<map>
  7. #include<queue>
  8. #include<algorithm>
  9. using namespace std;
  10. #define N 1006
  11. vector<int>g[N];
  12. vector<int>deep[N];
  13. int vis[N];
  14. void dfs(int st,int d){
  15. vis[st]=;
  16. deep[d].push_back(st);
  17. for(int i=;i<g[st].size();i++){
  18. int u=g[st][i];
  19. if(!vis[u]){
  20. dfs(u,d+);
  21. }
  22. }
  23. }
  24. int main()
  25. {
  26. int n;
  27. while(scanf("%d",&n)==){
  28.  
  29. for(int i=;i<=n;i++) {
  30. g[i].clear();
  31. deep[i].clear();
  32. }
  33. memset(vis,,sizeof(vis));
  34.  
  35. for(int i=;i<n;i++){
  36. int u,v;
  37. scanf("%d%d",&u,&v);
  38. g[u].push_back(v);
  39. g[v].push_back(u);
  40. }
  41. dfs(,);
  42.  
  43. int t=;
  44. while(deep[t].size()==) t++;
  45.  
  46. int num=deep[t].size();
  47. if(num+t!=n) {
  48. printf("NO\n");
  49. }else{
  50. printf("YES\n");
  51. }
  52.  
  53. }
  54. return ;
  55. }

hdu 5423 Rikka with Tree(dfs)的更多相关文章

  1. hdu 5423 Rikka with Tree(dfs)bestcoder #53 div2 1002

    题意: 输入一棵树,判断这棵树在以节点1为根节点时,是否是一棵特殊的树. 相关定义: 1.  定义f[A, i]为树A上节点i到节点1的距离,父节点与子节点之间的距离为1. 2.  对于树A与树B,如 ...

  2. (hdu)5423 Rikka with Tree (dfs)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5423 Problem Description As we know, Rikka is p ...

  3. HUD5423 Rikka with Tree(DFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5423 Rikka with Tree Time Limit: 2000/1000 MS (Java/O ...

  4. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

  5. 2017ACM暑期多校联合训练 - Team 1 1003 HDU 6035 Colorful Tree (dfs)

    题目链接 Problem Description There is a tree with n nodes, each of which has a type of color represented ...

  6. HDU 5423:Rikka with Tree Dijkstra算法

    Rikka with Tree  Accepts: 207  Submissions: 815  Time Limit: 2000/1000 MS (Java/Others)  Memory Limi ...

  7. HDU 5416 CRB and Tree (技巧)

    题意:给一棵n个节点的树(无向边),有q个询问,每个询问有一个值s,问有多少点对(u,v)的xor和为s? 注意:(u,v)和(v,u)只算一次.而且u=v也是合法的. 思路:任意点对之间的路径肯定经 ...

  8. ACM学习历程—HDU5423 Rikka with Tree(搜索)

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...

  9. hdu 1016 Prime Ring Problem(dfs)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. C++基础学习笔记----第十三课(操作符重载-下)

    本节主要讲使用成员函数重载操作符,包括[],=,(),->四种操作符的重载以及&&和||的问题. 类的成员函数进行操作符重载 基本概念 类的成员函数也可以进行操作符的重载.类的普 ...

  2. IOS学习笔记(四)之UITextField和UITextView控件学习

    IOS学习笔记(四)之UITextField和UITextView控件学习(博客地址:http://blog.csdn.net/developer_jiangqq) Author:hmjiangqq ...

  3. Java初转型-Maven入门

    原系列名:Maven学习总结(一) 原博文出自于:http://www.cnblogs.com/xdp-gacl/p/3498271.html 感谢! 一.Maven的基本概念 Maven(翻译为&q ...

  4. Chrome: Shockwave Flash isn't responding

    这个问题问 Google 解决得比较快,百度里尽是转载党的东西! 1. 到 chrome://settings/content 找到 Plug-ins 项目 2. 点击 Plug-ins 里的 Exc ...

  5. JavaScript原型,原型链 !

    js原型 问题:什么是js原型? js每声明一个function,都有prototype原型,prototype原型是函数的一个默认属性,在函数的创建过程中由js编译器自动添加. 也就是说:当生产一个 ...

  6. QueryFilter与SpatialFilter - 浅谈

    我们知道,GIS不仅仅有属性查询,还有空间查询.而 QueryFilter 对应于 属性查询,而 SpatialFilter 对应于 空间查询.

  7. hdu 1711 Number Sequence(KMP模板题)

    我的第一道KMP. 把两个数列分别当成KMP算法中的模式串和目标串,这道题就变成了一个KMP算法模板题. #include<stdio.h> #include<string.h> ...

  8. MongoDB 介绍及Windows下安装

    一.MongoDB简介 MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种.它在许多场景下可用于替代传统的关系型数据库或键/值存储方式.Mongo使用C++ ...

  9. Python同时向控制台和文件输出日志logging的方法 Python logging模块详解

    Python同时向控制台和文件输出日志logging的方法http://www.jb51.net/article/66756.htm 1 #-*- coding:utf-8 -*- 2 import ...

  10. WinFrm访问MVC数据

    WinFrm使用HttpWebRequest访问MVC中的Controller,以注册为例,客户端输入注册码后点击注册. WinFrm注册代码:代码中使用的是Post提交,UTF8编码方式. priv ...