传送门:Ubiquitous Religions

  • 许多次WA,贴上错的代码随时警示
  • 简单没多加修饰的并查集

【WA1】

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std; const int maxn = ;
int n,m;
int a,b,ans;
int pre[maxn]; void init()
{
for(int i=;i<=n;i++){
pre[i] = i;
}
} int findPre(int x){
if(x==pre[x]) return x;
findPre(pre[x]);
} bool isSame(int x,int y)
{
if(findPre(x)==findPre(y))
return true;
return false;
} void unite(int x,int y)
{
pre[y] = x; //y的上级变为x的祖先节点
} int main()
{
int kase = ;
while(scanf("%d%d",&n,&m)== && !(n==&&m==)){
init();
ans = ;
for(int i=;i<=m;i++){
scanf("%d%d",&a,&b);
unite(a,b);
}
for(int i=;i<=n;i++){
if(i==pre[i])
ans++;
}
printf("Case %d: %d\n",kase++,ans);
}
return ;
}

【WA2】

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std; const int maxn = ;
int n,m;
int a,b,ans;
int pre[maxn]; void init()
{
for(int i=;i<=n;i++){
pre[i] = i;
}
} int findPre(int x){
if(x != pre[x]){
pre[x] = findPre(pre[x]);
}
return pre[x];
} bool isSame(int x,int y)
{
if(findPre(x)==findPre(y))
return true;
return false;
} void unite(int x,int y)
{
pre[y] = x; //y的上级变为x的祖先节点
} int main()
{
int kase = ;
while(scanf("%d%d",&n,&m)== && !(n==&&m==)){
init();
ans = ;
for(int i=;i<=m;i++){
scanf("%d%d",&a,&b);
unite(a,b);
}
for(int i=;i<=n;i++){
if(i==pre[i])
ans++;
}
printf("Case %d: %d\n",kase++,ans);
}
return ;
}

【WA3】

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std; const int maxn = ;
int n,m;
int a,b,ans;
int pre[maxn]; void init()
{
for(int i=;i<=n;i++){
pre[i] = i;
}
} int findPre(int x){
if(x==pre[x]) return x;
findPre(pre[x]);
} bool isSame(int x,int y)
{
if(findPre(x)==findPre(y))
return true;
return false;
} void unite(int x,int y)
{
x = findPre(x);
y = findPre(y);
if(x==y) return;
ans--;
pre[y] = x; //y的上级变为x的祖先节点
} int main()
{
int kase = ;
while(scanf("%d%d",&n,&m)== && !(n==&&m==)){
init();
ans = n;
for(int i=;i<=m;i++){
scanf("%d%d",&a,&b);
unite(a,b);
}
printf("Case %d: %d\n",kase++,ans);
}
return ;
}

【AC】

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std; const int maxn = 50005;
int n,m;
int a,b,ans;
int pre[maxn]; void init()
{
for(int i=1;i<=n;i++){
pre[i] = i;
}
} int findPre(int x){
if(x != pre[x]){
pre[x] = findPre(pre[x]);
}
return pre[x];
} bool isSame(int x,int y)
{
if(findPre(x)==findPre(y))
return true;
return false;
} void unite(int x,int y)
{
x = findPre(x);
y = findPre(y);
if(x==y) return;
ans--;
pre[y] = x; //y的上级变为x的祖先节点
} int main()
{
int kase = 1;
while(scanf("%d%d",&n,&m)==2 && !(n==0&&m==0)){
init();
ans = n;
for(int i=1;i<=m;i++){
scanf("%d%d",&a,&b);
unite(a,b);
}
printf("Case %d: %d\n",kase++,ans);
}
return 0;
}

并查集——poj2524(入门)的更多相关文章

  1. poj 2524:Ubiquitous Religions(并查集,入门题)

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

  2. hrbustoj 1073:病毒(并查集,入门题)

    病毒Time Limit: 1000 MS Memory Limit: 65536 KTotal Submit: 719(185 users) Total Accepted: 247(163 user ...

  3. HDU 3047 带权并查集 入门题

    Zjnu Stadium 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3047 Problem Description In 12th Zhejian ...

  4. hdu畅通工程(并查集)

    Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道 ...

  5. 并查集_HDU 1232_畅通工程

    某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可). ...

  6. P2661 信息传递[最小环+边带权并查集]

    题目来源:洛谷 题目描述 有 n 个同学(编号为 1 到 n )正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为 i 的同学的信息传递对象是编号为 Ti​ 的同学. 游戏 ...

  7. POJ1611 && POJ2524 并查集入门

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 28293   Accepted: 13787 De ...

  8. hdu1272并查集入门

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  9. poj2524 Ubiquitous Religions(并查集)

    题目链接 http://poj.org/problem?id=2524 题意 有n个学生,编号1~n,每个学生最多有1个宗教信仰,输入m组数据,每组数据包含a.b,表示同学a和同学b有相同的信仰,求在 ...

随机推荐

  1. js邮箱验证,身份证验证,正则表达式

    邮箱验证: html部分: 邮箱验证:<input type="text" id="mail" value="" / onkeyup= ...

  2. Java基础知识(持续更新中...)

    1.成员变量:全局变量/字段(Field),不要称之为属性(错误)直接定义在类中,方法外面 1.类成员变量    使用static修饰的变量 2.实例成员变量 没用使用static修饰的变量 局部变量 ...

  3. mysql获取汉字首字母函数

    DELIMITER ;;CREATE FUNCTION `GET_FIRST_PINYIN_CHAR`(PARAM VARCHAR(255)) RETURNS VARCHAR(2) CHARSET u ...

  4. Oracle 反向索引(反转建索引) 理解

    一 反向索引 1.1 反向索引的定义 反向索引作为B-tree索引的一个分支,主要是在创建索引时,针对索引列的索引键值进行字节反转,进而实现分散存放到不同叶子节点块的目的. 1.2 反向索引针对的问题 ...

  5. ABAP术语-ABAP Dictionary

    ABAP Dictionary 原文链接:http://www.cnblogs.com/qiangsheng/archive/2007/12/07/986204.html Central redund ...

  6. 设置Vim编辑器里Tab的长度,行号

    使用Vim编辑器写脚本时,经常会遇到多重循环语句,习惯上会用tab键来补齐.这时设置tab键占用的长度,可以调节界面的松紧度,使其达到令人满意的效果. 在针对个别用户和所有用户来设置时,与编辑SSH相 ...

  7. 理解HBase

    1.HBase HBase: Hadoop Database,根据Google的Big Table设计 HBase是一个分布式.面向列族的开源数据库.HDFS为Hbase提供了底层的数据存储服务,Ma ...

  8. hadoop生态搭建(3节点)-06.hbase配置

    # http://archive.apache.org/dist/hbase/1.2.4/ # ==================================================== ...

  9. linux文件操作篇 (四) 目录操作

    #include <sys/stat.h>#include <unistd.h>#include <dirent.h> //创建文件夹 路径 掩码 int mkdi ...

  10. UVA 1593 Alignment of Code(紫书习题5-1 字符串流)

    You are working in a team that writes Incredibly Customizable Programming Codewriter (ICPC) which is ...