POJ 1308 Is It A Tree? (并查集)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 24237 | Accepted: 8311 |
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. 和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? (并查集)的更多相关文章
- 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 ...
- 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数据这就是唯一的区别....(因为这个瞎折腾了半天) 此题因为是为了熟悉并查集而刷,其实想了下其实 ...
- POJ 1308 Is It A Tree?
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18778 Accepted: 6395 De ...
- 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: 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 1417 - True Liars - [带权并查集+DP]
题目链接:http://poj.org/problem?id=1417 Time Limit: 1000MS Memory Limit: 10000K Description After having ...
- 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 ...
随机推荐
- F5 会话保持
1.什么是会话保持?在大多数电子商务的应用系统或者需要进行用户身份认证的在线系统中,一个客户与服务器经常经过好几次的交互过程才能完成一笔交易或者是一个请求的完成.由于这几次交互过程是密切相关的,服务器 ...
- 获取父进程ID
本程序主要功能是:获取某程序的ParentProcessID 直接上代码: // parent.cpp (Windows NT/2000) // // This example will show t ...
- CentOS 使用yum命令安装出现错误提示”could not retrieve mirrorlist http://mirrorlist.centos.org ***”
刚安装完CentOS,使用yum命令安装一些常用的软件,使用如下命令:yum –y install gcc. 提示如下错误信息: Loaded plugins: fastestmirror, refr ...
- HTML第七天学习笔记
今天主要是学习如何使用JS,第一个就是先是使用JS输出"Hello world" <!doctype html> <html lang="en" ...
- Hadoop on Mac with IntelliJ IDEA - 7 解决failed to report status for 600 seconds. Killing!问题
本文讲述作业在Hadoop 1.2.1完成map后ruduce阶段遇到failed to report status for 600 seconds. Killing!问题的解决过程. 环境:Mac ...
- Android 将文件保存到SD卡,从卡中取文件,及删除文件
//保存到SD卡 private static String sdState = Environment.getExternalStorageState(); private static S ...
- 数据结构复习:直接插入排序与二分插入排序的C++实现
1.直接插入排序 直接插入排序的过程可以理解为一个固定长度的数组被分为两个集合,即已排序集合和未排序. 开始时已排序集合为空,而未排序集合即为整个数组.当排序开始后插入一个对象,已排序集合元素数目加1 ...
- .Net连接到SAP【转载】
刚开始接触SAP了,感觉很陌生,清一色的TCode,不过里面的功能确实强大,不得不佩服啊,之前我一直是搞WinForm和WebForm的,现在能够接触到SAP那我还是想多学习一下,看了一下ABAP的语 ...
- CodeForces 173E Camping Groups 离线线段树 树状数组
Camping Groups 题目连接: http://codeforces.com/problemset/problem/173/E Description A club wants to take ...
- [Unity3D]Unity3D游戏开发之使用EasyTouch虚拟摇杆控制人物移动
大家好,欢迎大家关注我的博客,我是秦元培,我的博客地址是blog.csdn.net/qinyuanpei.今天呢,我们来一起学习在Unity3D中使用EasyTouch虚拟摇杆来控制人物移动.虽然Un ...