并查集——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有相同的信仰,求在 ...
随机推荐
- Linux学习——目录结构
在Linux当中,一切皆为文件,包括目录也属于文件.FHS(Filesystem Hierarchy Standard)的出现对文件目录系统做出了统一规范. Linux的目录结构: / - 根 /bi ...
- git使用过程的问题与解决办法
一.什么是Git Git是目前世界上最先进的分布式版本控制系统.工作原理 / 流程: Workspace:工作区Index / Stage:暂存区Repository:仓库区(或本地仓库)Remote ...
- aes 加密,解密
Javaaes加密: package com.sh.auth.util; import java.security.InvalidKeyException; import java.security. ...
- Vscode配置C++运行环境(2019/1//11更)并加入bits/stdc++.h头文件
因为重装系统,以前配置好的c++环境又没了.所以有要配置一遍. 1 下载mingw64或minw, 配置好环境变量:C://mingw64//bin; 在cmd下用g++ -v验证是否成功. 2. ...
- Linux 安装 python 指定版本--编译源码方式
自动化脚本: #!/bin/bash sudo apt-get update sudo apt-get install -y gcc make build-essential libssl-dev z ...
- background兼容IE9以下版本
.box { width:100%; height:80%; background: url('img/nav_bg.png') no-repeat; backgrou ...
- Jenkins 添加节点 java web方式
环境说明: 主节点:windows server 从节点:两台linux 1. windows server安装jenkins就不多说了,直接添加节点配置如下 2.全局安全配置,指定确认的端口后,记得 ...
- angularjs 自定义服务(serive,factory,provder) 以及三者的区别
1.Serive 服务:通过service方式创建自定义服务,相当于new的一个对象:var s = new myService();,只要把属性和方法添加到this上才可以在controller里调 ...
- 介绍三种PHP加密解密算法
PHP加密解密算法 这里主要介绍三种常用的加密解密算法:方法一: /** * @param $string 要加密/解密的字符串 * @param string $operation 类型,ENCOD ...
- APSchedule的练习使用
1 简介 APScheduler的全称是Advanced Python Scheduler.它是一个轻量级的 Python 定时任务调度框架.APScheduler 支持三种调度任务:固定时间间隔,固 ...