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. JavaScript的相关知识

      Oject.assign()   // Cloning an object var obj = { a: 1 }; var copy = Object.assign({}, obj); conso ...

  2. Android开发中常用的ListView列表的优化方式ViewHolder

    在Android开发中难免会遇到大量的数据加载到ListView中进行显示, 然后其中最重要的数据传递桥梁Adapter适配器是常用的,随着市场的需 求变化ListView'条目中的内容是越来越多这就 ...

  3. Python标准库os

    如果你希望自己的程序能够与平台无关的话,这个模块至关重要. os.name #'nt' for windows, 'posix' for linux/unix os.getcwd() #get cur ...

  4. Lazarus Coolbar and AnchroDocking

    在lazarus1.6里加载了AnchroDocking后,Coolbar突然不见了,找了好久没找到,原来在这里! 在AnchroDocking中可能是为了界面的最大化,默认是开始Toolbar 而关 ...

  5. ipv4的设置

    有段时间自己的网总是连不上,别人的都可以,因为公司又wifi,就将就着用wifi了,没有去查看原因,后来由于公司1网段大部分ip号被占用,系统要接入32路主机测试,每个主机都要分配ip,只好开辟2网段 ...

  6. appium 使用send_keys方法时报错: driver.find_element_by_id("com.hmkx.zgjkj:id/layout_search_bar_input").send_keys("123")

    新手 使用send_keys方法时一直报错,上网查这个方法的用法,看着大家都是这么写的啊,后来直接搜索 报错信息,搜索结果的针对性就清楚多了. 原来是seleium版本太高导致的问题. 可以先在cmd ...

  7. 让Android的WebView支持html里面的文件上传

    默认情况下,Android的webview是不支持<input type=file>的,点击没有任何反应,如果希望点击上传,弹出选择文件.图片的窗口,那就需要自定义一个WebChromeC ...

  8. 实现动画之CSS与JavaScript对比

    曾经某个时期,大多数开发者使用 jQuery 给浏览器中的元素添加动画.让这个淡化,让那个扩大,很简单.随着互动的项目越来越复杂,移动设备的大量增加,表现性能变得越来越重要.Flash 被抛弃,有天赋 ...

  9. bootstrap table分页(前后端两种方式实现)

    bootstrap table分页的两种方式: 前端分页:一次性从数据库查询所有的数据,在前端进行分页(数据量小的时候或者逻辑处理不复杂的话可以使用前端分页) 服务器分页:每次只查询当前页面加载所需要 ...

  10. python解析邮件的时候编码问题

    import sys import base64 import os import re s1 = '"=?UTF-8?B?56mG6Zi/5rWpKOWnnOW8iyk=?=" ...