题目链接: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(简单并查集)的更多相关文章

  1. POJ 2524 (简单并查集) Ubiquitous Religions

    题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰 简单并查集 //#define LOCAL #include <io ...

  2. poj1611 简单并查集

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 32781   Accepted: 15902 De ...

  3. 1213 How Many Tables(简单并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单并查集,统计单独成树的数量. 代码: #include <stdio.h> #i ...

  4. 【简单并查集】Farm Irrigation

    Farm Irrigation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tot ...

  5. ACM_“打老虎”的背后(简单并查集)

    “打老虎”的背后 Time Limit: 2000/1000ms (Java/Others) Problem Description: “习大大”自担任国家主席以来大力反腐倡廉,各地打击贪腐力度也逐步 ...

  6. poj1988 简单并查集

    B - 叠叠乐 Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:30000KB     64bit ...

  7. UVA - 1197 (简单并查集计数)

    Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...

  8. poj 2524 Ubiquitous Religions 一简单并查集

    Ubiquitous Religions   Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 22389   Accepted ...

  9. ACM_城市交通线(简单并查集)

    城市交通线 Time Limit: 2000/1000ms (Java/Others) Problem Description: A国有n座城市,编号为1~n,这n个城市之间没有任何交通线路,所以不同 ...

随机推荐

  1. 【转】WordPress转PHPCMS策略-数据库完美转换

    来源:http://www.sjyhome.com/php/wp-to-pc-sql.html WordPress的访问速度不可恭维?那就试试能够生成纯静态的PHPCMS,保证能够让你的网页访问速度有 ...

  2. C++公有派生

    C++中的公有派生: 1.格式:Class A:public B{...}: 例如我们先声明了一个基类:SafeIntArray,当我们要声明它的派生类IndexIntArray时,格式如下:

  3. windows添加linux 启动引导项

    需要软件: EasyBCD 下载地址: http://pan.baidu.com/s/1c20v1GO 在windows 下安装上述软件. 然后打开,语言的话现在中文即可. 可以看到如下画面: 1.选 ...

  4. bug-android之INSTALL_FAILED_NO_MATCHING_ABIS

    bug描述: 经常在网络上下载一些实例,自己研究 ,运行时不时会出现这个bug: Installation error: INSTALL_FAILED_NO_MATCHING_ABIS bug解决方案 ...

  5. mysql互换表中两列数据方法

    1.创建表及记录用于测试 ) unsigned ) ,) unsigned ,) unsigned NOT NULL COMMENT '现价', PRIMARY KEY (`id`) ) ENGINE ...

  6. 11.2---字符串数组排序,删除变位词(CC150)

    这道题主义的就是,要利用数组自带的sort函数. 此外,注意,利用hash来判断是否出现了. public static ArrayList<String> sortStrings(Str ...

  7. dict.items vs six.iteritems

    python2里面,dict.items返回的是数组,six.iteritems(dict)则返回生成器. 意味着,dict很大的时候,后者不占用内存. >>> import six ...

  8. 【leetcode】Permutations II

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  9. Python之队列queue模块使用 常见问题与用法

    python 中,队列是线程间最常用的交换数据的形式.queue模块是提供队列操作的模块,虽然简单易用,但是不小心的话,还是会出现一些意外. 1. 阻塞模式 import queue q = queu ...

  10. IDEA Error:java: 未结束的字符串文字

    首页 > 编程交流 > 基础篇 > IDEA Error:java: 未结束的字符串文字 201601-25 IDEA Error:java: 未结束的字符串文字   IDEA开发, ...