ACM学习历程—HDU5423 Rikka with Tree(搜索)
For a tree T, let F(T,i) be the distance between vertice 1 and vertice i.(The length of each edge is 1).
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 1 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?
For each testcase, the first line contains a number n(1≤n≤1000).
Then n−1 lines follow. Each line contains two numbers u,v(1≤u,v≤n) , which means there is an edge between u and v.
For the second testcase, this tree is similiar with the given tree:
题目要求的那个特殊的树其实就是一条直链,然后在链的末端接上若干节点,类似于扫帚的形状。
也就是最后一层的节点下方没有子节点,倒数第二层的节点下方可以有若干子节点,其余节点均只有一个子节点。
用dfs搜索每一层的节点进行判断即可。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <vector>
#define LL long long using namespace std; const int maxN = ; struct Edge
{
int to, next;
//int val;
}edge[maxN*]; int head[maxN], cnt; void addEdge(int u, int v)
{
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt;
cnt++;
} void initEdge()
{
memset(head, -, sizeof(head));
cnt = ;
} int n;
bool vis[maxN];
int maxDepth;
bool flag; bool input()
{
if (scanf("%d", &n) == EOF)
return false;
initEdge();
memset(vis, false, sizeof(vis));
maxDepth = ;
flag = true;
int u, v;
for (int i = ; i < n; ++i)
{
scanf("%d%d", &u, &v);
addEdge(u, v);
addEdge(v, u);
}
return true;
} void dfs(int now, int depth)
{
if (flag == false)
return;
int cnt = ;
vis[now] = true;
for (int i = head[now]; i != -; i = edge[i].next)
{
if (vis[edge[i].to])
continue;
dfs(edge[i].to, depth+);
cnt++;
}
if (cnt == )
return;
maxDepth = max(maxDepth, depth);
if (depth != maxDepth && cnt != )
flag = false;
} void work()
{
dfs(, );
if (flag)
printf("YES\n");
else
printf("NO\n");
} int main()
{
//freopen("test.in", "r", stdin);
while (input())
{
work();
}
return ;
}
ACM学习历程—HDU5423 Rikka with Tree(搜索)的更多相关文章
- ACM学习历程——HDU5202 Rikka with string(dfs,回文字符串)
Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...
- ACM学习历程—HDU 5534 Partial Tree(动态规划)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x ...
- ACM学习历程—HDU5422 Rikka with Graph(贪心)
Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...
- HDU-5423 Rikka with Tree。树深搜
Rikka with Tree 题意:给出树的定义,给出树相似的定义和不同的定义,然后给出一棵树,求是否存在一颗树即和其相似又与其不同.存在输出NO,不存在输出YES. 思路:以1号节点为根节点,我们 ...
- ACM学习历程——POJ3321 Apple Tree(搜索,线段树)
Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will ...
- ACM学习历程—POJ1088 滑雪(dp && 记忆化搜索)
Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...
- ACM学习历程—ZOJ3471 Most Powerful(dp && 状态压缩 && 记忆化搜索 && 位运算)
Description Recently, researchers on Mars have discovered N powerful atoms. All of them are differen ...
- ACM学习历程——HDU3333 Turing Tree(线段树 && 离线操作)
Problem Description After inventing Turing Tree, 3xian always felt boring when solving problems abou ...
- ACM学习历程——POJ3295 Tautology(搜索,二叉树)
Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some ...
随机推荐
- linux 下配置jdk
去java官方地址下载相应的源码包我下载的是1.8.0放在usr/local目录下 export JAVA_HOME=/usr/local/jdk1.8.0export PATH=$JAVA_HOME ...
- python 之前函数补充(__del__, item系列, __hash__, __eq__) , 以及模块初体验
__str__ : str(obj) , 需求必须实现了 __str__, 要求这个方法的返回值必须是字符串 str 类型 __repr__ (意为原型输出): 是 __str__ 的备胎( ...
- python解释器安装教程
1. 首先,打开python的官网:python.org 2. 首页downloads下打开, 3. 最上边是两个最新的版本,长期计划,推荐使用python3,如果长期打算用p3,默认使用最新版本.如 ...
- centOS-64位通过YUM源安装nginx
第一步:在 /etc/yum.repos.d/ 目录下,建立名叫nginx.repo的软件源配置文件. 文件 nginx.repo 的内容是: [nginx] name=nginx re ...
- R语言set.seed()函数介绍
set.seed(),该命令的作用是设定生成随机数的种子,种子是为了让结果具有重复性.如果不设定种子,生成的随机数无法重现.这个函数的主要目的,是让你的模拟能够可重复出现,因为很多时候我们需要取随机数 ...
- JSON JsonArray和JsonObject学习资料
资料地址: http://www.json.org/json-zh.html
- 【leetcode刷题笔记】Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 狄利克雷卷积&莫比乌斯反演
昨天刚说完不搞数论了,刚看到一个\(gcd\)的题目dalao用这个做了,虽然比正解麻烦,还是打算学一学了 数论函数: 数论函数的定义: 数论函数亦称算术函数,一类重要的函数,指定义在正整数集上的实值 ...
- python内置方法补充all
all(iterable) 版本:该函数在python2.5版本首次出现,适用于2.5以上版本,包括python3,兼容python3版本. 说明:如果iterable的所有元素不为0.''.Fals ...
- vmstat测试
vmstat测试:场景:tar -zvcf 2_104.tar.gz ./* 打包压缩3GB+文件 [root@localhost opt]# vmstat 1 1000procs --------- ...