hdu 5423 Rikka with Tree(dfs)
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 Aand 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?
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.
For each testcase, if the tree is special print "YES" , otherwise print "NO".
YES
NO
For the second testcase, this tree is similiar with the given tree:
根据题意可以构造出来的特殊树有三种情况(全是以结点1为根节点),,第一种是一条直线,第二种是一条直线末尾开花,第三种是直接开花。如图所示。然后就可以知道对于每一个深度的点数为1,1,1,...,x,0,0,...。然后dfs一遍找一下就可以了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
#define N 1006
vector<int>g[N];
vector<int>deep[N];
int vis[N];
void dfs(int st,int d){
vis[st]=;
deep[d].push_back(st);
for(int i=;i<g[st].size();i++){
int u=g[st][i];
if(!vis[u]){
dfs(u,d+);
}
}
}
int main()
{
int n;
while(scanf("%d",&n)==){ for(int i=;i<=n;i++) {
g[i].clear();
deep[i].clear();
}
memset(vis,,sizeof(vis)); for(int i=;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs(,); int t=;
while(deep[t].size()==) t++; int num=deep[t].size();
if(num+t!=n) {
printf("NO\n");
}else{
printf("YES\n");
} }
return ;
}
hdu 5423 Rikka with Tree(dfs)的更多相关文章
- hdu 5423 Rikka with Tree(dfs)bestcoder #53 div2 1002
题意: 输入一棵树,判断这棵树在以节点1为根节点时,是否是一棵特殊的树. 相关定义: 1. 定义f[A, i]为树A上节点i到节点1的距离,父节点与子节点之间的距离为1. 2. 对于树A与树B,如 ...
- (hdu)5423 Rikka with Tree (dfs)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5423 Problem Description As we know, Rikka is p ...
- HUD5423 Rikka with Tree(DFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5423 Rikka with Tree Time Limit: 2000/1000 MS (Java/O ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
- 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 ...
- HDU 5423:Rikka with Tree Dijkstra算法
Rikka with Tree Accepts: 207 Submissions: 815 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- HDU 5416 CRB and Tree (技巧)
题意:给一棵n个节点的树(无向边),有q个询问,每个询问有一个值s,问有多少点对(u,v)的xor和为s? 注意:(u,v)和(v,u)只算一次.而且u=v也是合法的. 思路:任意点对之间的路径肯定经 ...
- ACM学习历程—HDU5423 Rikka with Tree(搜索)
Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...
- hdu 1016 Prime Ring Problem(dfs)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- 大学生程序猿IT情书“2014爱的告白挑战赛”获奖名单及优秀情书展示系列之 - 【IT术语】情书+【搞笑另类】情书
经过专家评委们的层层精心评选和认真讨论,恭喜下面同学终于入选CSDN高校俱乐部"大学生程序猿IT情书2014爱的告白挑战赛活动"优胜者名单.获奖者将在本周内收到邮件通知.请依照邮件 ...
- Jenkins - 持续集成
Jenkins是一个开源软件项目,旨在提供一个开放易用的软件平台,使持续集成变成可能. Jenkins,之前叫做Hudson,是基于Java开发的一种持续集成工具,用于监控秩序重复的工作,包括: 1. ...
- IE下判断IE版本语法使用
先摆一下判断IE版本语法 <!--[if lte IE 6]> <![endif]--> IE6及其以下版本可见 <!--[if lte IE 7]> <![ ...
- Senparc.Weixin.MP SDK 微信公众平台开发教程 索引
Senparc.Weixin.MP SDK从一开始就坚持开源的状态,这个过程中得到了许多朋友的认可和支持. 目前SDK已经达到比较稳定的版本,这个过程中我觉得有必要整理一些思路和经验,和大家一起分享. ...
- python 下的数据结构与算法---5:递归(Recursion)
定义:递归就是不断分割整体成部分直到可以轻易解决分割出来的部分. 递归表达式三定律: 1:递归表达式必须有个最小单元 (最小单元既是停止递归调用以及能够直接运算的) 2:递归表达式在运算过程中 ...
- HTML5 Canvas前台压缩图片并上传到服务器
1.前台代码: <input id="fileOne" type="file" /> <input id="btnOne" ...
- Action<T> 和 Func<T> 委托
概述: 除了为每个参数和返回类型定义一个新委托类型之外,可以使用Action<T> 和 Func<T> 委托. Action<T> Action<T>委 ...
- static静态类与非静态类的区别
static静态类与非静态类的区别 1.在非静态类中可以有实例成员也可以有静态成员 2.在调用的时候需要使用对像名.实例成员调用(先要实例化,如person ps=new person(); ps. ...
- Java系列--第三篇 基于Maven的Android开发CAIO
学习要打好基础,这里用一个项目来学习一下Android的组件,参考网址为这个但不限于这个.有些东西的学习,理解三遍理论还不如一遍操作,所谓理论来自实践,实践是检验真理的唯一标准.所以,虽然看懂了那篇文 ...
- mysql在linux的安装