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 ...
随机推荐
- 集成StyleCop到Jenkins CI
这是集成完stylecop之后的Jenkins,可以看到code review结果随每个build变化的图表,Build History里面可以看到#150之前的build状态是unstable,这是 ...
- Linux编译安装RTL8192CU芯片驱动,使用TP_LINK wn823n无线网卡
前几天给自己的台式电脑安装了Window 7+CentOS 6.4 Linux双系统,发现在Windows 7下面可以正常使用TP_LINK wn823n无线网卡来连接无线网络,但是在Linux下面, ...
- IE=EmulateIE8和IE=IE8的区别
IE=8<meta http-equiv="X-UA-Compatible" content="IE=8" />This forces IE 8 t ...
- installshield 注册dll
function OnFirstUIAfter() STRING szTitle, szMsg1, szMsg2, szOpt1, szOpt2; NUMBER bOpt1, bOpt2; begin ...
- 转载:Flash AS3.0 加载外部资源(图片,MP3,SWF)的两种方式
Flash AS3.0 加载外部资源(图片,MP3,SWF)的两种方式 出自:http://www.cnblogs.com/top5/archive/2012/08/04/2623464.html 关 ...
- 解决 Cocos2d-x 中 Android.mk 手动添加源文件
转自:http://blog.csdn.net/ypfsoul/article/details/8909178 Makefile Android.mk 引发的思索 在我们编写 Android 平台 c ...
- 【机试题】c# 是否是素数,找出比它大的第一个素数
题目: 输入一个自然数 判断是否是素数,是素数则提示是素数,否则找出比它大的第一个素数 代码: Console.WriteLine("请输入任意一个自然数."); string n ...
- 【工作记录】c#操作win7注册表
这里讲一 C# 小列子(高手请绕过此地! ), 我们平时都是在xp开发比较多...不过现在很多开发人员也在win7下开发了. 下面是在 LocalMachine 下的 一下注册表操作 ,就不详说了 p ...
- uva 387 A Puzzling Problem (回溯)
A Puzzling Problem The goal of this problem is to write a program which will take from 1 to 5 puzz ...
- Android图片适配,drawable文件夹,低分辨率图片是否必要
我们知道,Android提供了几种不同分辨率的bitmap,来对应不同手机屏幕的密度.对应关系如下: xxhdpi:3.0 xhdpi: 2.0 hdpi: 1.5 mdpi: 1.0 ldpi: 0 ...