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.
 

跟小希的迷宫差不多,需要注意的是小希的迷宫是无向图,而本题是有向图。

判断是不是一棵树需要的条件如下:

1. 肯定满足只有一个树根,只有一个入度为0的点。
  2. 除了根每个点的入度只能为1
  3. 无环
  4. n个点只能有n-1个边

直接从小希的迷宫进行改的,不过本代码在poj  AC  ,但是HDU   Wrong Answer

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int set[];
bool vis[];
bool flag;
int find(int x)
{
return set[x]==x?x:set[x]=find(set[x]);
}
int main()
{
int a,b,k=;
while(scanf("%d%d",&a,&b))
{
int maxn=-;
if(a<&&b<)
break;
flag=true;
if(a==&&b==)
{
printf("Case %d is a tree.\n",++k);
continue;
}
for(int i=; i<; i++)
{
set[i]=i;
vis[i]=false;
}
while(a||b)
{
maxn=max(maxn,max(a,b));
vis[a]=true,vis[b]=true;
int x=find(a);
int y=find(b);
if(x!=y)
{
// if(x<y)
set[x]=y;
//else
//set[y]=x;
}
else
flag=false;
scanf("%d%d",&a,&b);
}
int ans=;
for(int i=; i<=maxn; i++)
{
if(vis[i] && set[i]==i)
ans++;
if(ans>)
flag=false;
}
if(flag)
printf("Case %d is a tree.\n",++k);
else
printf("Case %d is not a tree.\n",++k);
}
return ;
}

hdu 1325 && poj 1308 Is It A Tree?(并查集)的更多相关文章

  1. POJ 1308 Is It A Tree? (并查集)

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24237   Accepted: 8311 De ...

  2. HDU ACM 1325 / POJ 1308 Is It A Tree?

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  3. HDU 3081 Marriage Match II (二分图,并查集)

    HDU 3081 Marriage Match II (二分图,并查集) Description Presumably, you all have known the question of stab ...

  4. HDU 1325,POJ 1308 Is It A Tree

    HDU认为1>2,3>2不是树,POJ认为是,而Virtual Judge上引用的是POJ数据这就是唯一的区别....(因为这个瞎折腾了半天) 此题因为是为了熟悉并查集而刷,其实想了下其实 ...

  5. POJ 1308 Is It A Tree?和HDU 1272 小希的迷宫

    POJ题目网址:http://poj.org/problem?id=1308 HDU题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1272 并查集的运用 ...

  6. Hdu.1325.Is It A Tree?(并查集)

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  7. POJ 1308 Is It A Tree?

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18778   Accepted: 6395 De ...

  8. POJ 1308 Is It A Tree?--题解报告

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31092   Accepted: 10549 D ...

  9. POJ 1308 Is It A Tree? 解题报告

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32052   Accepted: 10876 D ...

随机推荐

  1. Xilinx------BUFG,IBUFG,BUFGP,IBUFGDS等含义以及使用

    转载-----BUFG,IBUFG,BUFGP,IBUFGDS等含义以及使用   目前,大型设计一般推荐使用同步时序电路.同步时序电路基于时钟触发沿设计,对时钟的周期.占空比.延时和抖动提出了更高的要 ...

  2. Android Studio 加载网络图片

    Android Studio是基于gradle的一个Android开发软件,在引用网络图片的时候需要连接第三方库,这里介绍 引用glide的方法. 一.在github页面搜索glide,点击第一个 二 ...

  3. 电商中的库存管理实现-mysql与redis

        库存是电商系统的核心环节,如何做到不少卖,不超卖是库存关心的核心业务问题.业务量大时带来的问题是如何更快速的处理库存计算. 此处以最简模式来讨论库存设计. 以下内容只做分析,不能直接套用,欢迎 ...

  4. Django多个中间件的执行顺序

    Django中的中间件是一个轻量级.底层的插件系统,可以介入Django的请求和响应处理过程,修改Django的输入或输出.中间件的设计为开发者提供了一种无侵入式的开发方式,增强了Django框架的健 ...

  5. (7)linux文件常用操作命令

    ls / 查看根目录下的子节点(文件夹和文件)信息ls -al -a是显示隐藏文件 -l是以更详细的列表形式显示 **切换目录cd /home cd .. 返回上一级 **创建文件夹mkdir aaa ...

  6. pyqt---------事件与信号处理

    pyqt:信号与槽的关系 GUI应用程序是事件驱动的. 事件主要由应用程序的用户生成. 但它们也可以通过其他手段产生,例如:网络连接,窗口管理器或定时器. 当我们调用应用程序的exec_()方法时,应 ...

  7. ecs主机被破解后下载的执行文件的脚本

    #!/bin/bashchmod 777 xiaochattr 777 haha/etc/init.d/iptables stop;service iptables stop;SuSEfirewall ...

  8. 分布式版本控制系统Git的安装和使用

    作业要求来自https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2097 GitHub远程仓库的地址:https://github.com/ ...

  9. faster rcnn源码阅读笔记3

  10. 微探eventlet.monkey_patch

    e ventlet.monkey_patch在运行时动态修改已有的代码,而不需要修改原始代码 在eventlet.monkey_patch中支持以下几种python原生库修改 eventlet.mon ...