hdu1325 Is It A Tree?(二叉树的推断)
Is It A Tree?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17520 Accepted Submission(s): 3939
There is exactly one node, called the root, to which no directed edges point.
Every node except the root has exactly one edge pointing to it.
There is a unique sequence of directed edges from the root to each node.
For example, consider the illustrations below, in which nodes are represented by circles and edges are represented by lines with arrowheads. The first two of these are trees, but the last is not.



In this problem you will be given several descriptions of collections of nodes connected by directed edges. For each of these you are to determine if the collection satisfies the definition of a tree or not.
the first integer identifies the node from which the edge begins, and the second integer identifies the node to which the edge is directed. Node numbers will always be greater than zero.
6 8 5 3 5 2 6 4
5 6 0 0
8 1 7 3 6 2 8 9 7 5
7 4 7 8 7 6 0 0
3 8 6 8 6 4
5 3 5 6 5 2 0 0
-1 -1
Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.
pid=1162" target="_blank" style="color:rgb(26,92,200); text-decoration:none">1162
pid=1198" target="_blank" style="color:rgb(26,92,200); text-decoration:none">1198
pid=1325" style="color:rgb(26,92,200); text-decoration:none">Statistic pid=1325" style="color:rgb(26,92,200); text-decoration:none">Note
这道题 我已经无力吐槽了、尽管和这道题(hdu1272)非常像。可也仅仅能是非常像、、这道题有方向。就这些。
1.0 0也是一棵树
2.不是-1 -1结束,而是两个数同一时候小于0结束,否则会导致TLE
3.我用并查集推断是否成环,然后推断顶点数是否等于边数+1。但是wa了。
。
4.最后我没实用并查集。不过推断出度和顶点数是否等于边数+1,A了
5.在这里我推断边数用的set容器。
由于在set容器里面假设有反复的数字,会仅仅记一次
我不说了。
看代码把、o(︶︿︶)o 唉
#include<stdio.h>
#include <string.h>
#include <set>
using namespace std;
int fa[1005],ru[1005];
int main()
{
set<int>s;
int a,b,edgs,t=1;
while(scanf("%d %d",&a,&b)!=EOF)
{
if(a<0&&b<0)
break;
if(a==0&&b==0)
{
printf("Case %d is a tree.\n",t++);
continue;
}
memset(ru,0,sizeof(ru));
ru[b]++;
s.clear();//一定要记得
edgs=1;
s.insert(a),s.insert(b);
while(1)
{
scanf("%d %d",&a,&b);
if(a==0&&b==0)
break;
edgs++,ru[b]++;
s.insert(a),s.insert(b);
}
int flag=1;
for(int i=0;i<1005;i++)
if(ru[i]>1)
flag=0;
if(s.size()==edgs+1&&flag)
printf("Case %d is a tree.\n",t++);
else
printf("Case %d is not a tree.\n",t++);
}
return 0;
}
hdu1325 Is It A Tree?(二叉树的推断)的更多相关文章
- Leetcode 101 Symmetric Tree 二叉树
判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...
- Leetcode 110 Balanced Binary Tree 二叉树
判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...
- [CareerCup] 4.7 Lowest Common Ancestor of a Binary Search Tree 二叉树的最小共同父节点
4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tr ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- UVA.548 Tree(二叉树 DFS)
UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...
- [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [LeetCode] 543. Diameter of Binary Tree 二叉树的直径
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...
- [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
随机推荐
- STMP服务器发送邮件,本地可以发送但是服务器一直发送不成功;
在官网上查看到信息 考虑到部分云服务商封禁了其内网对外 25 端口的访问, xxxxx 端口号: 2525 xxxxx 端口号: 587 然后,我换了一下端口号就行了,浪费了我三个小时时间,贼尴尬:
- V3的普通窗体控件的使用
1.文本: 属性: 标签对齐方式: Left Right Center 值对齐方式: Left Right Center 事件: 值改变事件 值加载事件 单击标题事件 键盘按下事件 获得焦点事 ...
- 简述UIDatePicker的用法
1.Locale 设置DatePicker的地区,即设置DatePicker显示的语言. 1.跟踪所有可用的地区,取出想要的地区 NSLog(@"%@", [NSLocale av ...
- [ SCOI 2009 ] 最长距离
\(\\\) \(Description\) 一个\(N\times M\)的网格图中有一些坏点,图是四联通的. 你至多可以拿走\(K\)个坏点,求拿走后联通的点对中欧几里得距离最大是多少. \(N, ...
- PHP内的包含语句(include(),require(),include_once(),require_once())
背景:继续总结PHP内的函数. 在一般的程序中,我们经常重复用到一些代码,但是如果只是简单的进行代码复制,则会增加代码的冗余度,不便于程序的运行.因此,我们可以把重复性的代码分离出来,以外部文件的方式 ...
- String数据类型转换
String是final类,提供字符串不可修改.强制类型转换,String类型无处不在.下面介绍一些常见的String数据类型转换. String数据类型转换成long.int.double.floa ...
- 011--c数组--排序--组成最大数
数组--排序--组成最大数 组成最大数 任意输入一个自然数,输出该自然数的各位数字组成的最大数.例如,输入 1593 ,则输出为 9531 . 输入: 自然数 n 输出: 各位数字组成的最大数 ...
- Html5 WebSocket详细介绍
什么是WebSocket?看过html5的同学都知道,WebSocket protocol 是HTML5一种新的协议.它是实现了浏览器与服务器全双工通信(full-duplex).HTML5定义了We ...
- demo__webpack
webpack 中使用的包更新非常频繁,使用方式可能很快就会改变,解决方式 看webapck文档 和 包的使用文档 看包的源码 其他... 环境 win10 + webstorm 2019.1.3 + ...
- 使用Robo 3T 软件管理MongoDB数据库如何执行命令行shell
比如使用命令行的方式查看数据库runoobdb中的sites集合(数据表)中的所有数据 1.在连接名的地方鼠标右键选择“open shell” 2.在出现的shell窗口中输入一下命令行,然后按ctr ...