poj1308(简单并查集)
题目链接:http://poj.org/problem?id=1308
题意:x, y 表示x 与 y连接,给出一波这样的数据,问这组数据能否构成树,即不能形成回路,不能有多个根节点;要注意可以是空树;
代码:
#include <iostream>
#include <stdio.h>
#define MAXN 1000000
using namespace std; int pre[MAXN], flag; int find(int x){
int r=x;
while(pre[r]!=r){
r=pre[r];
}
int i=x;
while(i!=r){
int gg=pre[i];
pre[i]=r;
i=gg;
}
return r;
} int jion(int x, int y){
int xx=find(x);
int yy=find(y);
if(xx!=yy){
pre[xx]=yy;
}else{
flag=;
}
} int main(void){
int a, b, t=;
while(scanf("%d%d", &a, &b)&&(a!=-&&b!=-)){
if(!a&&!b){
printf("Case %d is a tree.\n", t++);
}else{
flag=;
int Max=, c[MAXN], d[MAXN], i=;
Max=max(Max, max(a, b));
c[]=a, d[]=b;
int x, y;
while(scanf("%d%d", &x, &y)&&x&&y){
c[i]=x;
d[i]=y;
Max=max(Max, max(x, y));
i++;
}
for(int j=; j<=Max; j++){
pre[j]=j;
}
for(int j=; j<i; j++){
jion(c[j], d[j]);
}
int ans=;
for(int j=; j<i; j++){
if(c[j]==pre[c[j]]){
ans++;
}else if(d[j]==pre[d[j]]){
ans++;
}
}
if(ans> || flag==){
printf("Case %d is not a tree.\n", t++);
}else{
printf("Case %d is a tree.\n", t++);
}
}
}
return ;
}
poj1308(简单并查集)的更多相关文章
- POJ 2524 (简单并查集) Ubiquitous Religions
题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰 简单并查集 //#define LOCAL #include <io ...
- poj1611 简单并查集
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 32781 Accepted: 15902 De ...
- 1213 How Many Tables(简单并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单并查集,统计单独成树的数量. 代码: #include <stdio.h> #i ...
- 【简单并查集】Farm Irrigation
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
- ACM_“打老虎”的背后(简单并查集)
“打老虎”的背后 Time Limit: 2000/1000ms (Java/Others) Problem Description: “习大大”自担任国家主席以来大力反腐倡廉,各地打击贪腐力度也逐步 ...
- poj1988 简单并查集
B - 叠叠乐 Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:30000KB 64bit ...
- UVA - 1197 (简单并查集计数)
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...
- poj 2524 Ubiquitous Religions 一简单并查集
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 22389 Accepted ...
- ACM_城市交通线(简单并查集)
城市交通线 Time Limit: 2000/1000ms (Java/Others) Problem Description: A国有n座城市,编号为1~n,这n个城市之间没有任何交通线路,所以不同 ...
随机推荐
- centos systemctl指令
# systemctl #输出已激活单元 # systemctl list-units #输出已激活单元 # systemctl --failed #输出运行失败的单元 # systemctl lis ...
- 解决ScrollView里如果有动态更新的ChildView时会自动滚动到底部的方法
在这个ChildView的xml属性里加上 android:focusable="true" android:focusableInTouchMode="true&quo ...
- iOS中多线程原理与runloop介绍
一.线程概述 有些程序是一条直线,起点到终点:有些程序是一个圆,不断循环,直到将它切断.直线的如简单的Hello World,运行打印完,它的生命周期便结束了,像昙花一现那样:圆如操作系统,一直运行直 ...
- PHP中面相对象对象的知识点整理
面向对象 万物皆对象,将构成问题的事务分解到各个对象上,建立对象的目的不是为了完成一个工作,而是为了描述某个事务在解决问题中的行为,更符合人的思维习惯,代码重用性高,可扩展性. ___________ ...
- python gui之tkinter界面设计pythonic设计
ui的设计,控件id的记录是一件比较繁琐的事情. 此外,赋值和读取数据也比较繁琐,非常不pythonic. 有没有神马办法优雅一点呢?life is short. 鉴于控件有name属性,通过dir( ...
- 使用socket方式连接Nginx优化php-fpm性能
Nginx连接fastcgi的方式有2种:TCP和unix domain socket 什么是Unix domain socket?-- 维基百科 Unix domain socket 或者 IPC ...
- pycharm 2016 注册码
43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QTczWVlKIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNzaWduZWVOYW1lIjoiI ...
- SQL合并多行查询到一行
示例表 tb 数据如下 id value—————1 aa1 bb2 aaa2 bbb2 ccc 第一种 SELECT id, [val]=( SELECT [value] +',' FROM tb ...
- word to word
Question: For each word, you can get a list of neighbor words by calling getWords(String), find all ...
- hdu 1195
题意:就是给你n组的四位数,在一次变化中又一位数字可以变化,而变化的方式为加一减一或者是与隔壁的互换,注意,是每一个数字都可以, 求最少的变化次数到达目标的数字 一看这个就应该知道这是一个bfs的题目 ...