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 ...
随机推荐
- C++基础学习笔记----第十三课(操作符重载-下)
本节主要讲使用成员函数重载操作符,包括[],=,(),->四种操作符的重载以及&&和||的问题. 类的成员函数进行操作符重载 基本概念 类的成员函数也可以进行操作符的重载.类的普 ...
- IOS学习笔记(四)之UITextField和UITextView控件学习
IOS学习笔记(四)之UITextField和UITextView控件学习(博客地址:http://blog.csdn.net/developer_jiangqq) Author:hmjiangqq ...
- Java初转型-Maven入门
原系列名:Maven学习总结(一) 原博文出自于:http://www.cnblogs.com/xdp-gacl/p/3498271.html 感谢! 一.Maven的基本概念 Maven(翻译为&q ...
- Chrome: Shockwave Flash isn't responding
这个问题问 Google 解决得比较快,百度里尽是转载党的东西! 1. 到 chrome://settings/content 找到 Plug-ins 项目 2. 点击 Plug-ins 里的 Exc ...
- JavaScript原型,原型链 !
js原型 问题:什么是js原型? js每声明一个function,都有prototype原型,prototype原型是函数的一个默认属性,在函数的创建过程中由js编译器自动添加. 也就是说:当生产一个 ...
- QueryFilter与SpatialFilter - 浅谈
我们知道,GIS不仅仅有属性查询,还有空间查询.而 QueryFilter 对应于 属性查询,而 SpatialFilter 对应于 空间查询.
- hdu 1711 Number Sequence(KMP模板题)
我的第一道KMP. 把两个数列分别当成KMP算法中的模式串和目标串,这道题就变成了一个KMP算法模板题. #include<stdio.h> #include<string.h> ...
- MongoDB 介绍及Windows下安装
一.MongoDB简介 MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种.它在许多场景下可用于替代传统的关系型数据库或键/值存储方式.Mongo使用C++ ...
- Python同时向控制台和文件输出日志logging的方法 Python logging模块详解
Python同时向控制台和文件输出日志logging的方法http://www.jb51.net/article/66756.htm 1 #-*- coding:utf-8 -*- 2 import ...
- WinFrm访问MVC数据
WinFrm使用HttpWebRequest访问MVC中的Controller,以注册为例,客户端输入注册码后点击注册. WinFrm注册代码:代码中使用的是Post提交,UTF8编码方式. priv ...