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. [BTS] WCF-SAP adapter

    =================================== Exception has been thrown by the target of an invocation. (mscor ...

  2. Atitit.如何选择技术职业方向

    Atitit.如何选择技术职业方向 1. 原则是应该如下的应该从以下指标判断1 1.1. 技术的长寿性(长生命周期1 1.2. 技术的普适性(市场份额)1 1.3. **属于open体系还是封闭体系? ...

  3. paip.不同目录结构哈的文件批量比较

    paip.不同目录结构哈的文件批量比较 作者Attilax 艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/att ...

  4. win10 体验

    最近听说win10出了正式版,微软貌似在win10上投入了很大的期望,不知道到底怎么样,实践出真知,小编今天就亲自体验一下! 其实很多人对win8不满意,主要是因为win8 的兼容性不尽人意,小编的电 ...

  5. 负margin在布局中的运用

    一.左右栏宽度固定,中间栏宽度自适应 <!DOCTYPE html> <html> <head lang="en"> <meta char ...

  6. Java 泛型总结

    1. 泛型类 class Gen<T> { private T t; public T get(){ return t; } public void set(T argt){ t = ar ...

  7. PHP之負載均衡下的session共用

    最近忙於開發台灣運動彩券第四版的程式,所以已經很久沒有上來寫東西了,今天隨便寫點東西和大家分享. 首先說一下負載均衡,相信大家都知道負載均衡可以很好地解決網站大流量的問題,負載均衡就是把用戶的請求分發 ...

  8. Javacript和AngularJS中的Promises

    promise是Javascript异步编程很好的解决方案.对于一个异步方法,执行一个回调函数. 比如页面调用google地图的api时就使用到了promise. function success(p ...

  9. 多台linux服务器时间同步

    1,设置A机时间服务器: a,修改 /etc/ntp.conf,如下: # Undisciplined Local Clock. This is a fake driver intended for ...

  10. ngnix编译遇到的问题.

    总结:先后遇到libz库文件没有正确的链接和pcre库文件没有正确的链接 1./configure后提示需要zlib 2.locate zlib,系统中没有zlib的共享库so文件,但是有一些头文件, ...