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 ...
随机推荐
- 性能计数器自动收集-logman
1.在桌面云测试中,往往需要模拟并发连接中服务器的性能数据,这里主要介绍如何自动收集性能数据 2.创建xxxx.bat文件,文件内容如下: logman create counter test -cf ...
- Java基础知识强化40:StringBuffer类之StringBuffer的替换功能
1. StringBuffer的替换功能: public StringBuffer replace(int start, int end, String str): 2. 案例演示: p ...
- Python 正则表达试
字符串是编程时涉及到的最多的一种数据结构,对字符串进行操作的需求几乎无处不在.比如判断一个字符串是否是合法的Email地址,虽然可以编程提取@前后的子串,再分别判断是否是单词和域名,但这样做不但麻烦, ...
- 初探CSS
css基本框架 index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...
- Ubuntu 添加sudo用户
第一种方法: 添加sudo用户 当你安装Ubuntu的时候,它会自动添加第一个用户到sudo组,允许这个用户通过键入其自身帐户密 码来获得超级用户(root)身份.然而,系统不会再自动添加其他的用户到 ...
- PullToRefresh的使用
主界面↓ package com.wangzhen.pulltorefresh; import java.util.ArrayList; import java.util.List; import c ...
- java 线程池的用法
1.java自带的类ExecutorService用于提供线程池服务,可以一下方法初始化线程池: ExecutorService pool = Executors.newFixedThreadPool ...
- sql解释执行顺序
一.查询的逻辑执行顺序 (1) FROM left_table (3) join_type JOIN right_table (2) ON join_condition (4) WHERE where ...
- Arduino 板子 COM 接口找不到设备
复位 Arduino 板子 1.打开官方的blink程序. 2.重新拔插usb. 3.点下载. 4.按住左上角的复位按键不放. 5.等显示开始下载瞬间松开按键.把握住那个瞬间. 6.多试验几次看能 ...
- Hadoop学习历程(一、编译)
近期对Hadoop很感兴趣,下面是在CentOS系统上进行编译的分享 系统情况如下: 1. CentOS 6.2 64位 2. hadoop-2.2.0 3. jdk 1.6.0_31 ...