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. win10 64 + VS2010 + Opencv 2.4.9 + HIKVISION(海康)

    海康相机型号:DS-2CD2512F-IS 参考连接http://blog.csdn.net/wanghuiqi2008/article/details/31404571 先上效果图 其中,在连接时遇 ...

  2. ARCore中根据屏幕坐标计算射线的算法

    ARCore中提供了根据屏幕坐标.视口大小及view. project矩阵计算从屏幕坐标发射一条射线的方法,此方法用于3D拾取. class Ray { public final Vector3f o ...

  3. 二、Jmeter脚本开发

    目录 1.Jmeter协议录制 1.Jmeter协议录制 1.1 dboy进行录制 badboy下载地址:http://www.badboy.com.au/download/add badboy是一个 ...

  4. 安装和激活Office 2019

    有条件请支持正版!相比费尽力气找一个可能不太安全的激活工具,直接买随时随地更新的Office 365确实是最好的办法.暂时没有经济实力的,可以看看这篇文章.下载OTP工具 首先到Office Tool ...

  5. ansible 问题

    如下图,A服务器上用普通账号ansible有时就会报错,但有时却又正常,可以连接成功,用root账号执行ansible就完全没问题. 仅仅是这一台服务器有问题,其他都完全正常..ansible 文件删 ...

  6. jquery.validate和jquery.form配合实现验证表单后AJAX提交

    基础代码其实很简单,之后一点一点扩充.最终代码写在最后. 表单: <form action="@Url.Action("AddColumns","Cont ...

  7. 请求不同域的数据方法:requests Jsonp cors

    在需要访问不同域的接口的数据的时候,一般有两种方式: 第一种: 使用requests模块,在业务逻辑中直接访问别的域的接口,获取数据,然后将返回的数据显示到前端页面上; 这个时候,数据访问的流程是: ...

  8. JQEUERY案例

    案例效果: 点击显示全部奶粉品牌前: 点击后: 源码: <!DOCTYPE html><html><head> <meta charset="utf ...

  9. Tomcat常用设置及安全管理规范

    前言 随着公司内部使用Tomcat作为web应用服务器的规模越来越大,为保证Tomcat的配置安全,防止信息泄露,恶性攻击以及配置的安全规范,特制定此Tomcat安全配置规范.注意:  本文章从htt ...

  10. C++中的覆盖与隐藏(详细讲解)

    C++类中覆盖与隐藏一直是一个容易理解出错的地方,接下来我就详细讲解一下区别在何处 覆盖指的是子类覆盖父类函数(被覆盖),特征是: 1.分别位于子类和父类中 2.函数名字与参数都相同 3.父类的函数是 ...