传送门: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. Oracle 手工创建awr快照,获取报告

    Oracle 的自动化工具都是通过后台的进程调用相关的函数实现,而Oracle也允许用户通过包来手工调用这些函数,显然这样增加了工具的安全性,也提高了可操作性,使得DBA可以更灵活的使用这些函数来满足 ...

  2. SQL中 decode() 函数介绍

    decode() 函数的语法: Select decode(columnname,值1,翻译值1,值2,翻译值2,...值n,翻译值n,缺省值) From talbename Where … 其中:c ...

  3. vue 父子组件相互传值

    子传父 逻辑: 单击子组件的按钮 ,触发它的单击事件   通过 $emit 触发父级自定义事件 并传一个值给父级 <div id="id"> <h3>儿子 ...

  4. awk分隔符

    最近需要检测日志,shell中用到了awk,因为分割条件不止一个,并且包括了中括号.在此记录一下关于多分隔符并且包含中括号的情况 awk -F'[=,]|[][]+' '{print $6}'

  5. BZOJ2754: [SCOI2012]喵星球上的点名(AC自动机)

    Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 2816  Solved: 1246[Submit][Status][Discuss] Descript ...

  6. 爬虫——正则表达式re模块

    为什么要学习正则表达式 实际上爬虫一共就四个主要步骤: 明确目标:需清楚目标网站 爬:将所有的目标网站的内容全部爬下来 取:在爬下来的网站内容中去掉对我们没有用处的数据,只留取我们需要的数据 处理数据 ...

  7. mysql的数据操作和内置功能总结

    一.数据的增删改查 1.插入数据 a.插入完整数据(顺序插入) INSERT INTO 表名(字段1,字段2,字段3…字段n) VALUES(值1,值2,值3…值n); INSERT INTO 表名 ...

  8. Nginx反向代理 Laravel获取真实IP地址(PHP)

    使用VUE前后端分离开发 后端使用Laravel  想要获取到用户的真实IP地址 因为分离开发不同源跨域问题 所以只能进行前端Nginx反向代理 location /api { rewrite ^/a ...

  9. 20180803UnionPay银联支付

    LNMP环境下开发的银联支付(测试环境) 1.准备条件 a.银联支持API:https://open.unionpay.com/ajweb/help/api b.选择开发软件包 图示: c.我选择: ...

  10. SVM中的间隔最大化

    参考链接: 1.https://blog.csdn.net/TaiJi1985/article/details/75087742 2.李航<统计学习方法>7.1节 线性可分支持向量机与硬间 ...