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. RecyclerView中item无法充满的问题

    首先致谢:https://blog.csdn.net/yuanlvmao/article/details/51694211 咱们不是代码的生产者,只是代码的搬运工. 今天写了一个RecyclerVie ...

  2. Android中ViewPager动态创建的ImageView铺满屏幕

    ImageView imageView=new ImageView(context); imageView.setScaleType(ScaleType.FIT_XY);//铺满屏幕

  3. java与javascript之间json格式数据互转

    javascript中对象与字符串的互转 对象转为字符串:通过JSON.encode方法,这个是json.js里面的方法,引入到当前文件就可以了. 字符串转换为对象:①使用JSON.decode方法, ...

  4. Tcl之Lab1

    Task 1. Use help 1) What is the default switch for the redirect command? -file help -v redirect # or ...

  5. selenium的三种等待时间

    //隐式等待(20秒以内没哥一段时间就会去找元素,如果没找大也不会报错,过了20s才会报错) //driver.manage().timeouts().implicitlyWait(20, TimeU ...

  6. 扩增子图表解读3热图:差异菌、OTU及功能

    热图是使用颜色来展示数值矩阵的图形.通常还会结合行.列的聚类分析,以表达实验数据多方面的结果.  热图在生物学领域应用广泛,尤其在高通量测序的结果展示中很流行,如样品-基因表达,样品-OTU相对丰度矩 ...

  7. GeckoWebBrowser设置cookie

    var uri = new Uri("http://www.aa.com"); //often cookies are stored on domain level, so &qu ...

  8. drf07 过滤 排序 分页 异常处理 自动生成接口文档

    4. 过滤Filtering 对于列表数据可能需要根据字段进行过滤,我们可以通过添加django-fitlter扩展来增强支持. pip install django-filter 在配置文件sett ...

  9. WING IDE 快捷键

    工欲善其事必先利其器,所以我们无论使用什么编译器,都要熟悉一些快捷键. Ctrl+N新建文件 Ctrl+O 打开文件夹 Ctrl+W 关闭当前文件 Ctrl+S 保存文件 Ctrl+shif+S 另存 ...

  10. lua_note_01_lua介绍

    1. lua 1. lua 1.1. lua介绍 1.2. Lua 特性 1.3. 特点 1.4. Lua 应用场景 1.5. 环境搭建 1.6. VS lua 1.1. lua介绍 Lua 是一种轻 ...