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 ...
随机推荐
- 使用zTree控件制作的表格形式的树形+数据菜单
測试了一下,兼容ie7以上, chrome opera ff 不使用对方css /*------------------------------------- zTree Style version: ...
- 【BUG】Kewastunpackstats(): Bad Magic 1 (0x。。。。, 0)
Kewastunpackstats(): Bad Magic 1 (0x1108f7b87, 0) In Alert Logfile After Upgrading to 11.2.0.1 Appli ...
- jquery之onchange事件
$(function(){ $("#opreateHtml").window("close"); $("#deliveryGrid").da ...
- 小学生之KTV项目文档(bdqn)
第一步:创建数据库连接方法和打开方法和关闭方法! 1 public class DBHelper 2 { 3 private string str = "server=.;database= ...
- Jquery手机发送短信之后,进入倒计时状态
在做手机网站开发的时候,难免发生意外.这时候,就是你展示人格魅力的时候啦! 下面是自己写的一个发送验证码给手机之后,进入的一个倒计时的效果 js代码,我可是连<script type=" ...
- Lesson 2: Dive Into Typography (排版)
Lesson 2: Dive Into Typography (排版) 排版是字的艺术,是关于字的一切:字体.字号.行高.行长.字重(斜体/加粗/正常).字距(kerning).行距(leading) ...
- params参数的使用方法
params 将方法中实际参数列表中跟可变参数数组类型一致的类型,都处理为数组中的的元素 static void Main(string[] arr) { // int[] numbers={2,3, ...
- cas+tomcat+shiro实现单点登录-4-Apache Shiro 集成Cas作为cas client端实现
目录 1.tomcat添加https安全协议 2.下载cas server端部署到tomcat上 3.CAS服务器深入配置(连接MYSQL) 4.Apache Shiro 集成Cas作为cas cli ...
- TCP/IP 要点备忘
1. 3次握手/4次挥手过程,以及状态变化: 2. RTT,TTL,TOS(8位服务类型,最小延时.最大吞吐.最高可用.最小费用). 3. TimeWait(2msl)状态,防止最后一个ack丢失 4 ...
- VS2012 编写C++程序的时候DOS窗口一闪而过解决办法。
在我每次通过VS2012 运行C++程序的时候,DOS窗口在显示结果的时候都是一闪而过.这样让人没法观察输出的结果.经试验找到了一较好的解决办法: 首先包含头文件"stdlib.h" ...