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

Problem Description
A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties. 

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. 


 
Input
The input will consist of a sequence of descriptions (test cases) followed by a pair of negative integers. Each test case will consist of a sequence of edge descriptions followed by a pair of zeroes Each edge description will consist of a pair of integers;
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. 
 
Output
For each test case display the line ``Case k is a tree." or the line ``Case k is not a tree.", where k corresponds to the test case number (they are sequentially numbered starting with 1). 
 
Sample Input
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
 
Sample Output
Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.
 
Source
 
Recommend
 

pid=1325" style="color:rgb(26,92,200); text-decoration:none">Statistic | Submit | Discuss | 

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?(二叉树的推断)的更多相关文章

  1. Leetcode 101 Symmetric Tree 二叉树

    判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...

  2. Leetcode 110 Balanced Binary Tree 二叉树

    判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...

  3. [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 ...

  4. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  5. UVA.548 Tree(二叉树 DFS)

    UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...

  6. [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 ...

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

  8. [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  9. [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 ...

随机推荐

  1. C#用Microsoft.Office.Interop.Word进行Word转PDF的问题

    之前用Aspose.Word进行Word转PDF发现'\'这个字符会被转换成'¥'这样的错误,没办法只能换个方法了.下面是Microsoft.Office.Interop.Word转PDF的方法: p ...

  2. Laravel5.1学习笔记16 数据库2 查询构造器(这个不用看,不如用EloquentORM)

    Introduction Retrieving Results Aggregates Selects Joins Unions Where Clauses Advanced Where Clauses ...

  3. [ POI 2017 ] Podzielno

    \(\\\) \(Description\) \(B\)进制数,每个数字\(i(i\in [0,B-1])\)有\(A_i\)个.用这些数字组成一个最大的\(B\)进制数\(X\)(不能有前导零,不需 ...

  4. PHP开发之旅-表单验证

    一.创建表单 <form name = "login" method = "post" action="contact.php?action=l ...

  5. 使用adb命令提示端口被占用

    图是我的65535端口被占用了,一般adb默认使用的是5037端口##方式一   5037为adb默认端口,若5037端口被占用,查看占用端口的进程(使用命令netstat -aon|findstr ...

  6. HDU_1542_线段树【扫描线】

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  7. php省市区三级联动

    效果 步骤 前端:通过ajax请求获取数据,使用了jquery 页面一开始加载所有省份信息 ->当选择省下拉框后触发改变监听时间-change ->当选择市下拉框后触发改变监听时间-cha ...

  8. yum仓库配置ftpx协议

    [root@localhost ~]# iptables -F[root@localhost ~]# systemctl stop firewalld[root@localhost ~]# syste ...

  9. EasyUI, Dialog 在框架页(ifrmae)的Top页面弹出时,拖拽Dialog边缘(以改变窗口大小),UI界面被卡死的解决办法

    将Dialog的modal属性设置为true,可以解决卡死的问题(但会给用户使用体验带来影响) var par = { title: This.title, width: This.width, he ...

  10. JSONP代码收藏

    摘抄自jQuery,用于JSONP请求. var callback = 'callback_' + (new Date() - 0), url = 'http://localhost/', scrip ...