POJ 1308 Is It A Tree?
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 18778 | Accepted: 6395 |
Description
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
Output
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?的更多相关文章
- 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 并查集的运用 ...
- HDU 1325,POJ 1308 Is It A Tree
HDU认为1>2,3>2不是树,POJ认为是,而Virtual Judge上引用的是POJ数据这就是唯一的区别....(因为这个瞎折腾了半天) 此题因为是为了熟悉并查集而刷,其实想了下其实 ...
- 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 ...
- 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 ...
- POJ 1308 Is It A Tree? (并查集)
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24237 Accepted: 8311 De ...
- 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 ...
- POJ 1308 Is It A Tree?--题解报告
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31092 Accepted: 10549 D ...
- POJ 1308 Is It A Tree? 解题报告
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 32052 Accepted: 10876 D ...
- POJ 1308&&HDU 1272 并查集判断图
HDU 1272 I - 小希的迷宫 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
随机推荐
- 字符串匹配算法之BF(Brute-Force)算法
BF(Brute-Force)算法 蛮力搜索,比较简单的一种字符串匹配算法,在处理简单的数据时候就可以用这种算法,完全匹配,就是速度慢啊. 基本思想 从目标串s 的第一个字符起和模式串t的第一个字符进 ...
- [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 ...
- jenkins2 pipeline入门
本文通过简单的pipeline的实例和详细的讲解,能够学习基本pipeline的groovy用法,然后开始实现自己的pipeline job. 翻译和修改自:https://github.com/je ...
- 每日代码 - 6/26 lambda表达式
public class CartViewRender { public static CartView renderMyCartView(Cart cart){ ErrorCodeEnum erro ...
- Beta分布和Dirichlet分布
在<Gamma函数是如何被发现的?>里证明了\begin{align*} B(m, n) = \int_0^1 x^{m-1} (1-x)^{n-1} \text{d} x = \frac ...
- QWidget 实现 打破布局 或者 当前窗体内的 弹窗 (借助伪造实现)
but = QtWidgets.QToolButton(Dialog2) but.setText('**') but.setAutoRaise(True) layout.addWidget(but) ...
- UC脱茧蜕变,移动资讯市场格局再生变
日前,UC浏览器正式更名为UC,同时正式发布大数据驱动的独立资讯应用“UC头条”.而整个UC品牌也从工具类升级为优质资讯内容平台,并吹响了向“大数据新型媒体平台”进军的冲锋号.根据UC官方公布的数据显 ...
- Leetcode 19 Remove Nth Node From End of List 链表
删除从后往前数的第n个节点 我的做法是将两个指针first,second 先将first指向第n-1个,然后让first和second一起指向他们的next,直到first->next-> ...
- Android JNI HelloWorld实现
创建一个JNIDemo的Android工程 在项目下创建一个文件夹jni.(注意必须是jni目录) 在jni目录下创建两个文件:Android.mk 和 first_jni.c(.c文件的名字可以任意 ...
- 使用Nginx负载均衡搭建高性能.NETweb应用程序二
在文章<使用Nginx负载均衡搭建高性能.NETweb应用程序一>中,让我们对Nginx有了一个初步认识,下面我们将在windows平台下面使用Nginx演示集群部署我们的web应用. 一 ...