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. 字符串匹配算法之BF(Brute-Force)算法

    BF(Brute-Force)算法 蛮力搜索,比较简单的一种字符串匹配算法,在处理简单的数据时候就可以用这种算法,完全匹配,就是速度慢啊. 基本思想 从目标串s 的第一个字符起和模式串t的第一个字符进 ...

  2. [ACM_数学] Counting Solutions to an Integral Equation (x+2y+2z=n 组合种类)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27938#problem/E 题目大意:Given, n, count the numbe ...

  3. jenkins2 pipeline入门

    本文通过简单的pipeline的实例和详细的讲解,能够学习基本pipeline的groovy用法,然后开始实现自己的pipeline job. 翻译和修改自:https://github.com/je ...

  4. 每日代码 - 6/26 lambda表达式

    public class CartViewRender { public static CartView renderMyCartView(Cart cart){ ErrorCodeEnum erro ...

  5. Beta分布和Dirichlet分布

    在<Gamma函数是如何被发现的?>里证明了\begin{align*} B(m, n) = \int_0^1 x^{m-1} (1-x)^{n-1} \text{d} x = \frac ...

  6. QWidget 实现 打破布局 或者 当前窗体内的 弹窗 (借助伪造实现)

    but = QtWidgets.QToolButton(Dialog2) but.setText('**') but.setAutoRaise(True) layout.addWidget(but) ...

  7. UC脱茧蜕变,移动资讯市场格局再生变

    日前,UC浏览器正式更名为UC,同时正式发布大数据驱动的独立资讯应用“UC头条”.而整个UC品牌也从工具类升级为优质资讯内容平台,并吹响了向“大数据新型媒体平台”进军的冲锋号.根据UC官方公布的数据显 ...

  8. Leetcode 19 Remove Nth Node From End of List 链表

    删除从后往前数的第n个节点 我的做法是将两个指针first,second 先将first指向第n-1个,然后让first和second一起指向他们的next,直到first->next-> ...

  9. Android JNI HelloWorld实现

    创建一个JNIDemo的Android工程 在项目下创建一个文件夹jni.(注意必须是jni目录) 在jni目录下创建两个文件:Android.mk 和 first_jni.c(.c文件的名字可以任意 ...

  10. 使用Nginx负载均衡搭建高性能.NETweb应用程序二

    在文章<使用Nginx负载均衡搭建高性能.NETweb应用程序一>中,让我们对Nginx有了一个初步认识,下面我们将在windows平台下面使用Nginx演示集群部署我们的web应用. 一 ...