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 ...
随机推荐
- SVM支持向量机
支持向量机(Support Vector Machine,SVM)是效果最好的分类算法之中的一个. 一.线性分类器: 一个线性分类器就是要在n维的数据空间中找到一个超平面,通过这个超平面能够把两类数据 ...
- C#获取当前时间的各种格式
C#获取当前时间的各种格式 DateTime.Now.ToShortTimeString() DateTime dt = DateTime.Now; dt.ToString();//2005 ...
- GS与MS之间通信
GS与MS之间通信 注意GS与MS是两个线程,现在是每个map一个线程,他们之间是内部协议进行通信的,那既然是两个线程那如何通信呢,看了net进程通信这个就比较简单了 举个例子 m_pMap-> ...
- ASP.NET中指定自定义HTTP响应标头
新建一个类HideServerHeaderHelper,继承 IHttpModule,然后重写 OnPreSendRequestHeaders,Dispose,Init方法,如下代码所示 using ...
- vue前戏ES6
es6语法 es6语法:let和const: { var a=123; let b=234; } console.log(a); console.log(b); 浏览器里会只看到123; 而且还会抱一 ...
- 我的Android进阶之旅------>WindowManager.LayoutParams介绍
本文转载于: http://hubingforever.blog.163.com/blog/static/171040579201175111031938/ 本文参照自: http://develop ...
- Linux内核设计基础(九)之进程管理和调度
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/BlueCloudMatrix/article/details/30799225 在Linux中进程用 ...
- activiti踩坑
最近在学习activiti,偶然间遇到一个错误:加载引擎的时候报错,显示空指针错误,跟代码发现初始化配置文件返回为null.几经排查,可能是因为我发布流程后又清空了数据库数据导致的.然后我把表全部删除 ...
- R语言读取Excel文档
在R语言数据管理(三):数据读写一博文中,我曾写到有关读取xls.xlsx文件时一般将文档改成csv文件读取,这是一般做法.csv文件也有其缺点,修改较为麻烦,当文件数据较大时尤为明显.而生活中必不可 ...
- rails dependent
dependent 可以設定當物件刪除時,也會順便刪除它的 has_many 物件: class Event < ActiveRecord::Base has_many :attendees, ...