题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5423

Rikka with Tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 321    Accepted Submission(s): 162

Problem Description
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 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?
 
Input
There are no more than 100 testcases.
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
.
 
Output
For each testcase, if the tree is special print "YES" , otherwise print "NO".
 
Sample Input
3
1 2
2 3
4
1 2
2 3
1 4
 
Sample Output
YES
NO
此题满足条件的只有一种情况,即: 树从上到下的结点数依次为: 1, 1, 1, ,,,,x(x为任意). 也即是说,只有最后一层的结点数才能大于 1 .
dfs 求出每层的结点数, 就能判断出答案。
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
using namespace std; bool ok;
vector<int> a[];
int num[]; void dfs(int u, int fa, int d)
{
num[d]++;
int len = a[u].size();
for(int i=; i<len; i++)
{
if(a[u][i]==fa) continue;
dfs(a[u][i], u, d+);
}
} int main()
{
int n;
while(~scanf("%d", &n))
{
for(int i=; i<; i++)
a[i].clear();
memset(num, , sizeof(num));
for(int i=; i<n; i++)
{
int x, y;
scanf("%d%d", &x, &y);
a[x].push_back(y);
a[y].push_back(x);
}
ok = false;
dfs(, -, );
for(int i=; i<; i++)
{
if(num[i-]>&&num[i])
ok = true;
}
if(ok) printf("NO\n");
else printf("YES\n");
}
return ;
}
 

HUD5423 Rikka with Tree(DFS)的更多相关文章

  1. hdu 5423 Rikka with Tree(dfs)

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...

  2. hdu 5423 Rikka with Tree(dfs)bestcoder #53 div2 1002

    题意: 输入一棵树,判断这棵树在以节点1为根节点时,是否是一棵特殊的树. 相关定义: 1.  定义f[A, i]为树A上节点i到节点1的距离,父节点与子节点之间的距离为1. 2.  对于树A与树B,如 ...

  3. (hdu)5423 Rikka with Tree (dfs)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5423 Problem Description As we know, Rikka is p ...

  4. 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 ...

  5. ACM学习历程—HDU5423 Rikka with Tree(搜索)

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...

  6. 【CodeForces - 682C】Alyona and the Tree(dfs)

    Alyona and the Tree Descriptions 小灵决定节食,于是去森林里摘了些苹果.在那里,她意外地发现了一棵神奇的有根树,它的根在节点 1 上,每个节点和每条边上都有一个数字. ...

  7. codeforces 682C Alyona and the Tree(DFS)

    题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...

  8. Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View)

    Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...

  9. Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree)

    Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree) 深度优先搜索的解题详细介绍,点击 给定一个 N 叉树,找到其最大深度 ...

随机推荐

  1. 阿里BCG重磅报告《人工智能,未来致胜之道》

    阿里BCG重磅报告<人工智能,未来致胜之道> 阿里云研究中心.波士顿咨询公司以及Alibaba Innovation Ventures合作共同推出的<人工智能:未来制胜之道>这 ...

  2. 【jQuery UI 1.8 The User Interface Library for jQuery】.学习笔记.2.更换主题

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 快速搭建Redis缓存数据库

    之前一篇随笔——Redis安装及主从配置已经详细的介绍过Redis的安装于配置.本文要讲的是如何在已经安装过Redis的机器上快速的创建出一个新的Redis缓存数据库. 一.环境介绍 1) Linux ...

  4. 打开开源项目总得.md文件

    google了一些: 78 Tools for Writing and Previewing Markdown  http://mashable.com/2013/06/24/markdown-too ...

  5. HDU 4405:Aeroplane chess(概率DP入门)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Problem Description   Hzz loves ...

  6. YTU 3004: 栈的基本运算(栈和队列)

    3004: 栈的基本运算(栈和队列) 时间限制: 1 Sec  内存限制: 128 MB 提交: 32  解决: 10 题目描述 编写一个程序,实现顺序栈的各种基本运算,主函数已给出,请补充每一种方法 ...

  7. phpcms常用函数

    1../libs/functions/global.func.php    --------------------------------------------------字符串安全处理函数--- ...

  8. poj2482 Stars in Your Window

    此题可用线段树或静态二叉树来做. 考虑用线段树: 很容易想到先限定矩形横轴范围再考虑在此纵轴上矩形内物品总价值的最大值. 那么枚举矩形横轴的复杂度是O(n)的,考虑如何快速获取纵轴上的最大值. 我们不 ...

  9. poj1703 Find them, Catch them

    并查集. 这题错了不少次才过的. 分析见代码. http://poj.org/problem?id=1703 #include <cstdio> #include <cstring& ...

  10. 关于接收json以及使用json

    Common: FileIO.cs using System; using System.Collections.Generic; //using System.Linq; using System. ...