来源 hdu 1325

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.

和小希的迷宫有点像,但是他是有向的,要满足有向树的条件,那就是根节点没有被任何节点指向,其他的节点都只能被一个节点指向,否则就不对

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include <iomanip>
  5. #include<cmath>
  6. #include<float.h>
  7. #include<string.h>
  8. #include<algorithm>
  9. #define sf scanf
  10. #define pf printf
  11. #define mm(x,b) memset((x),(b),sizeof(x))
  12. #include<vector>
  13. #include<queue>
  14. #include<stack>
  15. #include<map>
  16. #define rep(i,a,n) for (int i=a;i<n;i++)
  17. #define per(i,a,n) for (int i=a;i>=n;i--)
  18. typedef long long ll;
  19. const ll mod=1e9+100;
  20. const double eps=1e-8;
  21. using namespace std;
  22. const double pi=acos(-1.0);
  23. const int inf=0xfffffff;
  24. int pre[100005],temp,visit[100005],num[100005];//num是保存被指向的数目
  25. int find(int x)
  26. {
  27. if(pre[x]==x) return x;
  28. return pre[x]=find(pre[x]);
  29. }
  30. void Union(int x,int y)
  31. {
  32. int a=find(x),b=find(y);
  33. if(a==b)
  34. {
  35. temp=1;
  36. return ;
  37. }
  38. pre[b]=a;
  39. }
  40. int main()
  41. {
  42. int x,y,ans=1;
  43. while(1)
  44. {
  45. temp=0;
  46. mm(visit,0);
  47. mm(num,0);
  48. int sum=0;
  49. rep(i,0,100005) pre[i]=i;
  50. while(1)
  51. {
  52. sf("%d%d",&x,&y);
  53. if(x+y==0) break;
  54. if(x+y<0) return 0;
  55. Union(x,y);
  56. visit[x]=1;visit[y]=1;
  57. num[y]++;
  58. }
  59. rep(i,0,100005)
  60. {
  61. if(visit[i]&&pre[i]==i) sum++;//判断是否是森林
  62. if(num[i]>1) temp=1;被指向超过两次
  63. }
  64. if(temp||sum>1)
  65. pf("Case %d is not a tree.\n",ans++);
  66. else
  67. pf("Case %d is a tree.\n",ans++);
  68. }
  69. }

B - Is It A Tree?的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. SAP CRM 树视图(TREE VIEW)

    树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...

  3. 无限分级和tree结构数据增删改【提供Demo下载】

    无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...

  4. 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>

    在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...

  5. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  6. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  7. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  8. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  9. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  10. Tree树节点选中及取消和指定节点的隐藏

    指定节点变色 指定节点隐藏 单击节点 未选中则选中该节点 已选中则取消该节点 前台: 1.HTML <ul id="listDept" name="listDept ...

随机推荐

  1. C# SpinWait 实现

    其实SpinWait的code 非常简单,以前看过很多遍,但是从来都没有整理过,整理也是再次学习吧. 我们先看看SpinWait的一些评论或者注意点吧:如果等待某个条件满足需要的时间很短,而且不希望发 ...

  2. 关于Python ,requests的小技巧

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/xie_0723/article/details/52790786 关于 Python Request ...

  3. WPF如何为程序添加splashScreen(初始屏幕)

    一.考虑到大部分的splashscreen其实都只是一个图片,所以最简单的做法是,先导入一张图片,然后设置它的生成操作为“splash screen” 二.通过程序设置SplashScreen pub ...

  4. postgresql ltree类型

    最近一个月使用Postgresql的时候,经常遇到ltree的数据,感觉有些别扭,可是有绕不过去.今天决心整理一下,以后使用方便一些. 一.简介 ltree是Postgresql的一个扩展类型,由两位 ...

  5. NIO-新的io操作方式

    1.BIO带来的挑战 BIO即阻塞IO,不管是磁盘IO,还是网络IO,数据在写入OutputStream或者从InputStream读取时都有可能发生阻塞,一旦有阻塞,当前线程将会被挂起,即线程进入非 ...

  6. CentOS 6下 Oracle11gR2 设置开机自启动

    [1] 更改/etc/oratab # This file is used by ORACLE utilities. It is created by root.sh # and updated by ...

  7. 用ctrl+鼠标滚动调节字体大小

    如此设置之后,按住ctrl+鼠标滚动,可以放大和变小代码的字号.

  8. Windows下libjpeg-trubo-1.5.3编译(VS2015)

    简述 https://libjpeg-turbo.org/的网站上是有已经编译好的版本下载的,但是VC下是使用的VC10.0编译的.虽然在VC14.0下也能用,但是我还是需要编译一个VC14.0版本的 ...

  9. C++11 并发指南九(综合运用: C++11 多线程下生产者消费者模型详解)

    前面八章介绍了 C++11 并发编程的基础(抱歉哈,第五章-第八章还在草稿中),本文将综合运用 C++11 中的新的基础设施(主要是多线程.锁.条件变量)来阐述一个经典问题——生产者消费者模型,并给出 ...

  10. 利用jsPDF有效减轻报表型应用服务器的IO负载

    1.利用jsPDF在客户端浏览器上生成pdf文档 使用这种方法可以有效减轻服务器的压力,但是对于国际化,此库任然存在的问题:该库不支持unicode,生成中文文档会乱码,官方也有描述这个问题,详情请参 ...