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. 大数据~说说Hadoop

    Hadoop是一个由Apache基金会所开发的分布式系统基础架构. 用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力进行高速运算和存储.  Hadoop实现了一个分布式文件系 ...

  2. C# 将结构体转为字节流的方式

    1. 将基础类型转为byte数组存储 private byte[] CreateNetDataByteStream(ushort system, ushort host, ushort type, b ...

  3. ssh项目导入报the import javax.servlet cannot be resolved

    在做javaWeb项目时,我们经常会出现丢失包的情况,如下图所示的错误,我们应该怎么解决呢? 根据网上教程向工程中加入tomcat的servlet-api.jar和jsp-api.jar的包 此时项目 ...

  4. iOS 从xib中加载自定义视图

    想当初在学校主攻的是.NET,来到公司后,立马变成java开发,之后又跳到iOS开发,IT人这样真的好么~~  天有不测风云,云还有变幻莫测哎,废话Over,let's go~ 新学iOS开发不久,一 ...

  5. HashMap中capacity、loadFactor、threshold、size等概念的解释

    约定 约定前面的数组结构的每一个格格称为桶 约定桶后面存放的每一个数据称为bin bin这个术语来自于JDK 1.8的HashMap注释. size size表示HashMap中存放KV的数量(为链表 ...

  6. SSL协议提供的服务

    SSL协议提供的服务主要有: 1)认证用户和服务器,确保数据发送到正确的客户机和服务器: 2)加密数据以防止数据中途被窃取: 3)维护数据的完整性,确保数据在传输过程中不被改变.

  7. linux调试环境时常用的命令 及 常识

    1.查找文件或文件夹 所在的目录 sudo find / -name your_filename 示例: wangju@wangju-HP--G4:~$ sudo find / -name .jenk ...

  8. mybatis 项目配置

    第一:environments MyBatis 支持多个环境,可以任意配置: 第二:transactionManager MyBatis 支持两种类型的事务管理器:JDBC 和 MANAGED(托管) ...

  9. EF-Lamdba

    一丶基本语法 var userList=db.set<table>().where(c=>c.id=="001"&&c.userName.Cont ...

  10. DNS详细解析过程【转】

    转自:http://blog.csdn.net/crazw/article/details/8986504 先说一下DNS的几个基本概念: 一. 根域 就是所谓的“.”,其实我们的网址www.baid ...