(并查集)Is It A Tree? --POJ--1308
链接:
http://poj.org/problem?id=1308
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#problem/N
代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std; #define N 100005 int f[N];
bool r[N]; void IN()
{
memset(r, false, sizeof(r));
for(int i=; i<N; i++)
f[i]=i;
} int Find(int x)
{
while(x!=f[x])
{
x=f[x];
}
return f[x];
} int main()
{
int a, b, fa, fb, i,sum=, flag1=, flag2=, k=, MIN, MAX; IN();
while(scanf("%d%d", &a, &b), a>= || b>=)
{
if(a== && b==)
{
if(sum==)
{
printf("Case %d is a tree.\n", k++);
continue;
}
int z = Find(MIN);
for(i=MIN; i<=MAX; i++)
{
if(r[i])
{
if(z!=Find(i)) // 根(父)节点不同
{
flag2=;
break;
}
}
} if(flag1 || flag2)
printf("Case %d is not a tree.\n", k++);
else
printf("Case %d is a tree.\n", k++);
IN();
sum=, flag1=, flag2=;
}
else
{
sum++;
r[a] = , r[b] = ;
MIN = min(MIN, min(a ,b)), MAX = (MAX, max(a, b)); fa = Find(a), fb = Find(b);
if(fa != fb)
f[fa] = fb;
else //构成了环
flag1=;
} }
return ;
}
(并查集)Is It A Tree? --POJ--1308的更多相关文章
- Is It A Tree? POJ - 1308(并查集判树)
Problem Description A tree is a well-known data structure that is either empty (null, void, nothing) ...
- 并查集+关系的传递(poj 1182)
题目:食物链 题意:给定一些关系.判断关系的正确性,后给出的关系服从之前的关系: 思路:难点不在并查集,在于关系的判断,尤其是子节点与根节点的关系的判断: 这个关系看似没给出,但是给出子节点与父节点的 ...
- Is It A Tree? POJ - 1308
题意: 题目给你一组单向边,当遇到输入0 0就证明这是一组边,当遇到-1 -1就要停止程序.让你判断这是不是一棵树 题解: 题目很简单,但是程序要考虑的很多 1.因为是一颗树,所以肯定不能出现环,这个 ...
- 类似区间计数的种类并查集两题--HDU 3038 & POJ 1733
1.POJ 1733 Parity game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5744 Accepted: ...
- (并查集 建立关系)Parity game -- POJ -1733
链接: http://poj.org/problem?id=1733 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...
- (并查集 贪心思想)Supermarket -- POJ --1456
链接: http://poj.org/problem?id=1456 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...
- POJ 2492 并查集应用的扩展
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Descri ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
- [kuangbin带你飞]专题五 并查集
并查集的介绍可以看下https://www.cnblogs.com/jkzr/p/10290488.html A - Wireless Network POJ - 2236 An earthquake ...
- ACM/ICPC 之 并查集-食物链(POJ1182)
并查集的经典题型,POJ上题目还是中文= =,一般看到中文题都会感觉不太简单,这道题的数学归纳用得比较多,可以简化代码,挺有意思的. 同类型的题目还有POJ1703,比这个要简单,想了解并查集基本介绍 ...
随机推荐
- form表单中的encType属性
enctype 属性规定在发送到服务器之前应该如何对表单数据进行编码,它有三个值 1.application/x-www-form-urlencoded:表单数据被编码为名称/值对.这是默认的编码方式 ...
- Technology, globalisation and the squeeze on good jobs
Technology, globalisation and the squeeze on good jobs技术与全球化冲击好工作“Highest stock market EVER! Jobs ar ...
- maven GroupId 和ArtifactId通常填什么
GroupID是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构.ArtifactID就是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称.
- JSP复习(part 4)
3.5.2 重定向网页 使用response对象中的sendRedirect()方法实现一个重定向到另一个页面. 例如: response.sendRedirect(“”login_ok.jsp“”) ...
- Centos7 創建快捷方式
usr/share/applications 創建文件 Gogland.desktop [Desktop Entry]Version=1Type=ApplicationName=GoglandGene ...
- cin中函数的作用
cin是istream类的对象,它是从标准输入设备(键盘)获取数据,程序中的变量通过流提取符">>"从流中提取数据.流提取符">>"从流 ...
- 38. Count and Say (String; DP)
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- C++强制转换
static_cast,dynamic_cast, const_cast, reinterpret_cast dynamic_cast比static_cast多了安全检测,判断源和目标有无继承被继承关 ...
- maven 发布打包部署 命令
一.配置好jdk 二.下载安装maven http://maven.apache.org/download.cgi 三.添加环境变量 1. 添加 M2_HOME 和 MAVEN_HOME 环境变量到 ...
- linux下一些常用系统命令
查看系统打开的文件数 lsof|wc -l 查看当前目录下的文件数 find -type f | wc -l 查看某个目录下的文件数,注意这里/home包括其所有子目录 find /home -typ ...