并查集——poj2524(入门)
- 许多次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(入门)的更多相关文章
- poj 2524:Ubiquitous Religions(并查集,入门题)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23997 Accepted: ...
- hrbustoj 1073:病毒(并查集,入门题)
病毒Time Limit: 1000 MS Memory Limit: 65536 KTotal Submit: 719(185 users) Total Accepted: 247(163 user ...
- HDU 3047 带权并查集 入门题
Zjnu Stadium 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3047 Problem Description In 12th Zhejian ...
- hdu畅通工程(并查集)
Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道 ...
- 并查集_HDU 1232_畅通工程
某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可). ...
- P2661 信息传递[最小环+边带权并查集]
题目来源:洛谷 题目描述 有 n 个同学(编号为 1 到 n )正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为 i 的同学的信息传递对象是编号为 Ti 的同学. 游戏 ...
- POJ1611 && POJ2524 并查集入门
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 28293 Accepted: 13787 De ...
- hdu1272并查集入门
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- poj2524 Ubiquitous Religions(并查集)
题目链接 http://poj.org/problem?id=2524 题意 有n个学生,编号1~n,每个学生最多有1个宗教信仰,输入m组数据,每组数据包含a.b,表示同学a和同学b有相同的信仰,求在 ...
随机推荐
- 课时60.CSS的固定格式(掌握)
CSS就是用来设置样式的,美化界面的 如何验证? 打开一个京东首页 删除掉css样式 发现页面变得非常难看 由此我们验证了一个说法,css就是用来美化界面的 1.格式: <style type= ...
- File zilla远程连接服务器报错:服务器发回了不可路由的地址,使用服务器地址代替
百度的答案都是:更改Filezilla设置,编辑-设置-连接-FTP-被动模式,将“使用服务器的外部ip地址来代替”改为“回到主动模式”即可.但问题没有解决!!! 由于使用的是阿里云的服务器.安全组里 ...
- php-5.6.26源代码 - hash存储结构 - 添加
添加 , (void *)module, sizeof(zend_module_entry), (void**)&module_ptr){ // zend_hash_add 定义在文件“php ...
- 微信小程序终于审核过了
终于,我做的微信小程序审核结束了,虽然被退回来两次,但是第三次还是审核通过了! 加油骚年,相信自己!! 有什么问题可以评论告诉我!!
- Laravel5.x 封装的上传图片类
图片缩放需要用conposer安装 ImageManagerStatic类 可参考下面的地址安装: https://www.jb51.net/article/128159.htm 控制器里: 控制器里 ...
- 基于pyecharts的IT各行业薪资展示
我们的项目是一个信息采集系统,采集的是51job招聘网站,我爬取了Python,Java,C++,PHP还有北京各地区的职位数量,以及经验要求,和学历要求等等. 网页头; <!DOCTYPE h ...
- ArrayList底层原理
ArrayList底层采用数组实现,访问特别快,它可以根据索引下标快速找到元素.但添加插入删除等写操作效率低,因为涉及到内存数据复制转移. ArrayList对象初始化时,无参数构造器默认容量为10, ...
- c#获取当前运行程序所在的目录
C#获取项目程序及运行路径的方 1.asp.net webform用“Request.PhysicalApplicationPath获取站点所在虚拟目录的物理路径,最后包含“\”: 2.c# wi ...
- 10-mongodb启动错误
1.error信息 python@ubuntu:~$ mongod --22T17:: I CONTROL [initandlisten] MongoDB starting : pid= port= ...
- 你真的了解React吗
https://zhufengzhufeng.github.io/zhufengreact/index.html#t21.%E4%BB%80%E4%B9%88%E6%98%AFReact?