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个城市之间没有任何交通线路,所以不同 ...
随机推荐
- Android Fragment完全解析,关于碎片你所需知道的一切
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8881711 我们都知道,Android上的界面展示都是通过Activity实现的, ...
- Hibernate get和load区别
1.从返回结果上对比:load方式检索不到的话会抛出org.hibernate.ObjectNotFoundException异常get方法检索不到的话会返回null 2.从检索执行机制上对比: ...
- iOS开发——UI进阶篇(五)通知、代理、kvo的应用和对比,购物车
一.通知 1.通知中心(NSNotificationCenter)每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信任何一个对象都可以向 ...
- 第一款支持容器和云部署的开源数据库Neo4j 3.0
导读 Neo4j 3.0.0 正式发布,这是 Neo4j 3.0 系列的第一个版本.此版本对内部架构进行了全新的设计:提供给开发者更强大的生产力:提供更广阔的部署选择.Neo4j 3.0 被认为是世界 ...
- Unity手游之路<十一>资源打包Assetbundle
http://blog.csdn.net/janeky/article/details/17652021 在手游的运营过程中,更新资源是比不可少的.资源管理第一步是资源打包.传统的打包可以将所有物件制 ...
- Linux之Shell脚本计算命令行的所有和
问题:设计一个Shell脚本:求命令行上所有整数和 Shell代码: 调用add.sh脚本结果:
- 剑指Offer 跳台阶
题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 解题思路: f(n)=f(n-1)+f(n-2); f(1)=1,f(2)=2; AC代码 ...
- 7.2---蚂蚁相遇问题(CC150)
public class Ants { public double antsCollision(int n) { // write code here return (1 - Math.pow(0.5 ...
- SVN里常见的图标及其含义
- 已忽略版本控制的文件.可以通过Window → Preferences → Team → Ignored Resources.来忽略文件.A file ignored by version con ...
- 引用外部css文件
<link type="text/css" rel="stylesheet" href="http://files.cnblogs.com/91 ...