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

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.
题目大意:给出一些数字对,代表父节点和子节点,输入以0 0结束,问这些节点能否构成一棵树。
解题方法:判断能否构成一棵树的条件:1.空树是一棵树。2.树中不存在回路。3.森林不是树。可以用并查集判断是否存在回路,然后用并查集扫描一遍,看是否所有节点的最顶端父节点相同,不同则为森林。
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std; typedef struct
{
int rank;
int parent;
}UFSTree; UFSTree Set[];
int Stack[];//用于保存每个节点对中的父节点,以检测是否为森林 void MakeSet()
{
for (int i = ; i < ; i++)
{
Set[i].rank = ;
Set[i].parent = i;
}
} int FindSet(int x)
{
if (x == Set[x].parent)
{
return x;
}
else
{
return FindSet(Set[x].parent);
}
} void UnionSet(int x, int y)
{
x = FindSet(x);
y = FindSet(y);
if (Set[x].rank > Set[y].rank)
{
Set[y].parent = x;
}
else
{
Set[x].parent = y;
if (Set[x].rank == Set[y].rank)
{
Set[y].rank++;
}
}
} int main()
{
int x, y, nCase = , top = ;
bool flag = true;
MakeSet();
while(scanf("%d%d", &x, &y) != NULL && x != - && y != -)
{
while()
{
if (x == && y == )
{
break;
}
Stack[top] = x;
top++;
//如果两个节点处于同一集合,则不能形成一棵树
if (FindSet(x) == FindSet(y))
{
flag = false;
}
else
{
UnionSet(x, y);
}
scanf("%d%d", &x, &y);
}
if (flag)
{
//查询每个节点对中的父节点,看所有节点的最顶端父节点是否相同,不相同则为森林
for (int i = ; i < top - ; i++)
{
int n1 = FindSet(Stack[i]);
int n2 = FindSet(Stack[i + ]);
if (n1 != n2)
{
flag = false;
break;
}
}
}
if (flag)
{
flag = true;
printf("Case %d is a tree.\n", nCase++);
}
else
{
flag = true;
printf("Case %d is not a tree.\n", nCase++);
}
MakeSet();
top = ;
}
return ;
}

POJ 1308 Is It A Tree?的更多相关文章

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

  2. HDU 1325,POJ 1308 Is It A Tree

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

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

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

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

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

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

    Is It A Tree? 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/M Description A tree is a w ...

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

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

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

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

  9. POJ 1308&&HDU 1272 并查集判断图

      HDU 1272 I - 小希的迷宫 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. js操作textarea方法集合

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  2. Could not load file or assembly 'System.Data.SQLite' or one of its dependencies. 试图加载格式不正确的程序。

    出现上述问题的原因是,所加载的程序集中有32位的,也有64位的,IIS 7 程序池 在Windows下.Net FrameWork是64位的,要想正确使用,需要对程序池进行配置.如下图所示:

  3. Apache CXF Webservice入门

    1.步骤一览 关于CXF的介绍请移步官网.百科,这里仅供初次使用者入门. 2.步骤详情 2.1.环境准备 apache-cxf-3.0.0.zip下载 jdk1.7.0_51 Eclipse4.3.0 ...

  4. iOS系统app崩溃日志手动符号化

    iOS系统app崩溃日志手动符号化步骤: 1.在桌面建立一个crash文件夹,将symbolicatecrash工具..crash文件..dSYM文件放到该文件夹中 a.如何查询symbolicate ...

  5. duilib进阶教程 -- 改进窗口拖动 (12)

    现在大家应该都知道caption="0,0,0,32",是指示标题栏区了吧,如果想要整个窗口都能拖动呢? 那直接把高度改成和窗口一样不就得了~O(∩_∩)O~ 嗯,这样是可以,比如 ...

  6. paip.信用卡账单处理系统功能vO22

    paip.信用卡账单处理系统功能vO22 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/att ...

  7. RAR和ZIP:压缩大战真相

    转:http://fqd2eh4y.blog.163.com/blog/static/69195855200801035015857 前言--王者归来? 等待足足两年之久,压缩霸主WinZip终于在万 ...

  8. javascript基础知识复习一

    JavaScript 一.数据类型 A.String B.Number C.boolean  1.undefined.false.null.0.“”这五个返回的都是false: 2.NAN==NAN返 ...

  9. MyEclipse使用总结——设置MyEclipse开发项目时使用的JDK

    安装好MyEclipse之后,在MyEclipse中开发项目时,默认使用的是MyEclipse是自带的JDK,如下图所示: 如果我们需要使用自己安装好的JDK,那么就需要在MyEclipse中重新设置 ...

  10. AStar算法(转载)

    以下的文章来至http://blog.csdn.net/debugconsole/article/details/8165530,感激这位博主的翻译,可惜图片被和谐了,所以为方便阅读,我重新把图片贴上 ...