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

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. 和HDU 1272一模一样,除了改下输出其他什么都不用改。
 #include <iostream>
#include <cstdio>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std; const int SIZE = ;
int FATHER[SIZE],RANK[SIZE];
vector<int> S; void ini(int);
int find_father(int);
bool unite(int,int);
bool same(int,int);
int main(void)
{
int a,b,count = ;
bool flag; while(scanf("%d%d",&a,&b) && (a != - && b != -))
{
flag = true;
if(a == && b == )
{
printf("Case %d %s\n",++ count,flag ? "is a tree." : "is not a tree.");
continue;
}
S.clear();
S.push_back(a);
S.push_back(b);
ini(SIZE - );
flag = unite(a,b);
while(scanf("%d%d",&a,&b) && (a || b))
{
flag &= unite(a,b);
S.push_back(a);
S.push_back(b);
}
for(int i = ;i < S.size();i ++)
flag &= same(S[],S[i]);
printf("Case %d %s\n",++ count,flag ? "is a tree." : "is not a tree.");
} return ;
} void ini(int n)
{
for(int i = ;i <= n;i ++)
{
FATHER[i] = i;
RANK[i] = ;
}
} int find_father(int n)
{
if(n == FATHER[n])
return n;
return FATHER[n] = find_father(FATHER[n]);
} bool unite(int x,int y)
{
x = find_father(x);
y = find_father(y); if(x == y)
return false;
if(RANK[x] < RANK[y])
FATHER[x] = y;
else
{
FATHER[y] = x;
if(RANK[x] == RANK[y])
RANK[x] ++;
}
return true;
} bool same(int x,int y)
{
return find_father(x) == find_father(y);
}

POJ 1308 Is It A Tree? (并查集)的更多相关文章

  1. hdu 1325 && poj 1308 Is It A Tree?(并查集)

    Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a ...

  2. 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 并查集的运用 ...

  3. HDU 1325,POJ 1308 Is It A Tree

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

  4. POJ 1308 Is It A Tree?

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

  5. 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 ...

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

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

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

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

  8. POJ 1417 - True Liars - [带权并查集+DP]

    题目链接:http://poj.org/problem?id=1417 Time Limit: 1000MS Memory Limit: 10000K Description After having ...

  9. 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 ...

随机推荐

  1. IOS深入学习(19)之View object

    1 前言 本章主要介绍了View视图对象,包括了其属性,视图间关系和CALayer的简介. 英文原文:http://blog.csdn.net/developer_zhang/article/deta ...

  2. WSO2 ESB

    什么是WSO2 ESB? WSO2 ESB是一个轻量级的易于使用的企业服务资源总线.WSO2 ESB允许系统管理员和SOA架构师,消息路由,虚拟化,中介,转换,日志记录,任务调度,负载平衡,失败了路由 ...

  3. Js制作点击输入框时默认文字消失的效果

    (从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期 2014-02-17) 为了提高用户体验和易用度,一些设计师会对网页中用户经常用的东西进行优化,比如输入框.一般的输入框是怎样优化的呢 ...

  4. iOS开发-用预处理指令代替注释

    有时候我们在调试程序的时候,想不执行某一段代码或者某些方法,我们会用注释,但是这样看上去就会让你的代码显示“很绿”,此时我们可以用一下代码 #if 0 // 你不想执行的代码 #endif 这样就可以 ...

  5. VPW协议解析

    http://www.dpfdoctor.net/content/?220.html SAE J1850 VPW协议也是OBD II标准中的一种,通常应用于GM车系中. VPW英文全称是Variabl ...

  6. BZOJ 3930: [CQOI2015]选数 递推

    3930: [CQOI2015]选数 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pro ...

  7. C#中简单调用MD5方法以及MD5简介

    MD5简介:          MD5的全称是Message-Digest Algorithm 5,在90年代初由MIT的计算机科学实验室和RSA Data Security Inc发明,经MD2.M ...

  8. iOS7与iOS8的比較

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl1c2h1d2VpMDIyNA==/font/5a6L5L2T/fontsize/400/fill/I0 ...

  9. Approaching the Fun Factor in Game Design

    I recently did some research on this and talked to Dr. Clayton Lewis (computer Scientist in Residenc ...

  10. 深入研究Clang(三) libclang

    作者:史宁宁 如今的Clang,不不过一个编译器前端,同一时候也能够作为一个库使用.作为一个库使用的时候,能够用它去分析C/C++/ObjectC语言代码,能够分析源代码得到AST,也能够获取已经分析 ...