HUD5423 Rikka with Tree(DFS)
题目链接: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
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
.
2 3
2 3
1 4
- #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)的更多相关文章
- hdu 5423 Rikka with Tree(dfs)
Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...
- 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 ...
- 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 ...
- ACM学习历程—HDU5423 Rikka with Tree(搜索)
Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...
- 【CodeForces - 682C】Alyona and the Tree(dfs)
Alyona and the Tree Descriptions 小灵决定节食,于是去森林里摘了些苹果.在那里,她意外地发现了一棵神奇的有根树,它的根在节点 1 上,每个节点和每条边上都有一个数字. ...
- codeforces 682C Alyona and the Tree(DFS)
题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...
- Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View)
Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...
- Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree)
Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree) 深度优先搜索的解题详细介绍,点击 给定一个 N 叉树,找到其最大深度 ...
随机推荐
- LUA笔记之字符串
LUA提供了自动数值转换, 基本上说是乱来也不为过, 就当做是不熟悉数据结构吧, 例子: print("10" + 1) --> 11 print("10 + 1& ...
- 启动管理软件服务器时,提示midas.dll错误
首先确认系统以及管理软件目录内是否有midas.dll文件,如果没有,请复制或下载midas.dll到相应目录.系统默认路径为:'c:\windows\system32\' 然后依次打开“开始菜单”内 ...
- Install MongoDB on Red Hat Enterprise, CentOS, Fedora, or Amazon Linux
Install MongoDB on Red Hat Enterprise, CentOS, Fedora, or Amazon Linux¶ Overview Use this tutorial t ...
- HDU 5961:传递(暴搜)
http://acm.hdu.edu.cn/showproblem.php?pid=5961 题意:中文题意.给出两个图,判断这个两个图是否都是传递的.注意一下传递的定义要看清,一开始没看清连样例都看 ...
- 对已有的2个一维数组,譬如说A[],B[],经过最少循环找出2个数组重复的元素。
import java.util.Arrays; /** * Created by ccc on 16-4-27. */ public class Test { public static void ...
- tar 报错gzip: stdin: not in gzip format
今天在linux下 用tar -zxvf xxx.tar.bz2 然后就报这个错. gzip: stdin: not in gzip formattar: Child returned status ...
- Gson心得小笔记
又和往常一样看项目的时候遇到了点新的东西,至少对我来说是个新的东西吧.Gson 废话不多说.个人认为Gson主要用来实现对象和json之间的转换. 例如有个person对象,想要把这个对象转化为jso ...
- Balance
Balance Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11947 Accepted: 7464 Description ...
- DataRow数组转换DataTable
public DataTable ToDataTable(DataRow[] rows) { if (rows == null || rows.Length == 0) return null; Da ...
- 使用opencv自带的融合函数
[wiki,blog]使用opencv自带的融合函数 [wiki,blog]使用opencv自带的融合函数 /*M/////////////////////////////////////////// ...