传送门: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. JDBC连接数据库时错误提示的解决方案汇总

    今天在连接JDBC时,出现了错误 最开始的URL是这样写的 Connection conn = DriverManager.getConnection("jdbc:mysql://local ...

  2. Select 语句执行顺序以及如何提高Oracle 基本查询效率

    今天把这几天做的练习复习了一下,不知道自己写得代码执行的效率如何以及要如何提高,于是乎上网开始研究一些材料,现整理如下: 首先,要了解在Oracle中Sql语句运行的机制.以下是sql语句的执行步骤: ...

  3. 设置Oracle数据库开机自启动

    1.oracle 用户下 修改$ORACLE_HOME/bin/dbstart   vim /home/oracle/database/product/12c/db_1/bin/dbstart 将OR ...

  4. 序列(Sequence)创建、使用、修改和删除

    序列(Sequence)是用来生成连续的整数数据的对象.序列常常用来作为主键中增长列,序列中的可以升序生成,也可以降序生成. 语法结构:创建序列 CREATE SEQUENCE sequence_na ...

  5. C++练习--实现客户机(CLIENT)类

    实现客户机(CLIENT)类. 定义字符型静态数据成员SeverName,保存其服务器名称: 整型静态数据成员ClientNum,记录已定义的客户数量: 定义静态函数ChangeSeverName() ...

  6. 【原创】从 列表的重复 到 用sum展开二层嵌套列表将子元素合并

      转载请注明出处:https://www.cnblogs.com/oceanicstar/p/9517159.html     ★像R语言里头有rep函数可以让向量的值重复,在python里面可以直 ...

  7. python selenuim如何判断下拉框是否加载出来,超过时间不再等待

    s_flag = True time_start = time.time() while s_flag: doc = etree.HTML(unicode.encode(driver.page_sou ...

  8. 【c学习-7】

    #include /*#include"test31.c"*/ //定义阶乘函数 /* int fac(int n){ //定义寄存器存储变量 register int i ,f= ...

  9. Mysql导出表结构和数据

    导出数据库 -- 导出dbname表结构 mysqldump -uroot -p123456 -d dbname > dbname.sql -- 导出dbname表数据 mysqldump -u ...

  10. 二、Django需要的知识点

    1.请求(request): 客户端到服务器端. 响应(response):服务器端到客户端. HTTP/1.1 协议共定义了 8 种请求方式,分别是: OPTIONS. HEAD. GET. POS ...