poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)
http://poj.org/problem?id=2236
由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由于硬件的限制,一台电脑和另一台电脑能够相连当他们之间的距离小于d,或者还有一台电脑当中介,分别与两台电脑相连。
在修复的过程中,工作者会有两种操作,修复电脑和询问电脑a和电脑b是否相连。当询问的时候输出答案。
因为输入数据很大,需要快速判断电脑a和电脑b相连,所以自然想到用并查集。
初始时候 全部电脑都是损坏的,那么每修复一次,就需要把与当前电脑距离小于d并且已经修复的电脑连接起来,询问的时候直接判断即可。
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
const int maxn = ;
struct node
{
int x,y;
}p[maxn];
int par[maxn],flag[maxn]; //作为是否修复的标记
int n;
double dis[maxn][maxn]; //存储两点之间的距离
double d;
void init()
{
for(int i=;i<=n;i++) par[i]=i;
} int find(int x)
{
return x==par[x]?x:par[x]=find(par[x]);
} void unite(int x,int y)
{
x=find(x);
y=find(y);
if(x!=y)
par[x]=y;
} void distance(int a,int b)
{
dis[a][b]=dis[b][a]=sqrt(1.0*(p[a].x-p[b].x)*(p[a].x-p[b].x)+1.0*(p[a].y-p[b].y)*(p[a].y-p[b].y));
} void solve(int x)
{
for(int i=;i<=n;i++)
{
if(i!=x&&flag[i]&&dis[x][i]<=d)
unite(x,i);
}
} int main()
{
//freopen("a.txt","r",stdin);
scanf("%d%lf",&n,&d);
init();
memset(flag,,sizeof(flag));
memset(dis,,sizeof(dis));
for(int i=;i<=n;i++)
scanf("%d%d",&p[i].x,&p[i].y);
for(int i=;i<=n;i++) //预处理所有两点之间的距离
for(int j=i+;j<=n;j++)
distance(i,j);
char s[];
int a,b;
while(~scanf("%s",s))
{
if(s[]=='O')
{
scanf("%d",&a);
flag[a]=;
solve(a);
}
else if(s[]=='S')
{
scanf("%d%d",&a,&b);
if(find(a)==find(b)) printf("SUCCESS\n");
else printf("FAIL\n");
}
}
return ;
}
http://poj.org/problem?id=1611
在大学里有n个学生(0-n-1)和m个社团,每个学生可以加多个社团,但是因为SARS的流行,学校需要统计出有多少病毒携带者,如果一个社团有一个携带者,那么整个社团都是 病毒携带者,现在已知0号学生是携带者,给出m个社团的信息,求出有多少人感染。
基础的并查集,只要增加一个rank[]用来统计每一个组的人数,这样在unite的时候累加合并过来的人数,最后输出0号所在的组的人数。
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = ;
int par[maxn],rank[maxn]; void init(int n)
{
for(int i=;i<n;i++)
{
par[i]=i;
rank[i]=;
}
} int find(int x)
{
return x==par[x]?x:par[x]=find(par[x]);
} void unite(int x,int y)
{
x=find(x);
y=find(y);
if(x!=y)
{
par[x]=y;
rank[y]+=rank[x];
}
} int main()
{
int n,m,k,a,b;
while(~scanf("%d%d",&n,&m))
{
if(n==&&m==) break;
init(n);
for(int i=;i<m;i++)
{
scanf("%d",&k);
for(int j=;j<k;j++)
{
scanf("%d",&a);
if(j==)
{
b=a;
}
else unite(a,b);
}
}
printf("%d\n",rank[find()]);
}
return ;
}
http://poj.org/problem?id=2524
n个学生,每个学生都只有一种信仰,给出m条关系,统计最多有多少种信仰。
简单并查集,读入关系后合并即可。
#include <cstdio>
const int maxn = ;
int n,count,par[maxn]; void init(int n)
{
for(int i=;i<=n;i++)
{
par[i]=i;
}
} int find(int x)
{
return x==par[x]?x:par[x]=find(par[x]);
} void unite(int x,int y)
{
x=find(x);
y=find(y);
if(x!=y)
{
par[x]=y;
count--;
}
} int main()
{
int m,a,b,j=;
while(~scanf("%d%d",&n,&m))
{
if(n==&&m==) break;
count=n;
init(n);
for(int i=;i<m;i++)
{
scanf("%d%d",&a,&b);
unite(a,b);
}
printf("Case %d: %d\n",j++,count);
}
return ;
}
poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)的更多相关文章
- poj 2524:Ubiquitous Religions(并查集,入门题)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23997 Accepted: ...
- POJ 2524 Ubiquitous Religions (并查集)
Description 当今世界有很多不同的宗教,很难通晓他们.你有兴趣找出在你的大学里有多少种不同的宗教信仰.你知道在你的大学里有n个学生(0 < n <= 50000).你无法询问每个 ...
- poj 2524 Ubiquitous Religions(并查集)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23168 Accepted: ...
- POJ 2524 Ubiquitous Religions 【并查集】
解题思路:输入总人数 n,和m组数据:即和杭电畅通工程相类似,对这m组数据做合并操作后,求最后一共有多少块区域. #include<stdio.h> int pre[50001]; int ...
- poj 2524 Ubiquitous Religions(简单并查集)
对与知道并查集的人来说这题太水了,裸的并查集,如果你要给别人讲述并查集可以使用这个题当做例题,代码中我使用了路径压缩,还是有一定优化作用的. #include <stdio.h> #inc ...
- POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集
POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题五 并查集 POJ 2236 Wireless Network
题意: 一次地震震坏了所有网点 现在开始修复它们 有N个点 距离为d的网点可以进行通信 O p 代表p点已经修复 S p q 代表询问p q之间是否能够通信 思路: 基础并查集 每次修复一个点重新 ...
- [并查集] POJ 2236 Wireless Network
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 25022 Accepted: 103 ...
- poj 2236:Wireless Network(并查集,提高题)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 16065 Accepted: 677 ...
随机推荐
- UML 用例图,时序图,活动图的定义以及区别
1.用例图,时序图,活动图的定义 1.用例图: 用例图描述了系统提供的一个功能单元.用例图的主要目的是帮助开发团队以一种可视化的方式理解系统的功能需求,包括基于基本流程的"角色" ...
- MongoDB 基础
1. 安装 mongodb-win32-x86_64-2008plus-2.6.12-signed.msi,下载地址 https://www.mongodb.com/download-center#c ...
- mysql解决错误的方法-MySQL日志
1.使用ps -ef|grep mysql查询是否有与MySQL相关的僵尸进程,如果有则强制杀掉 2.在配置文件my.cnf中配置启动错误日志: log_error = /var/log/mysql/ ...
- PHPStorm+PHP5.6+WIN7+IIS7
文件下载 以下为参考网址,如无法打开或变动,请自行搜索,获取最新版本文件请行搜索 PHP Manager:http://www.iis.net/downloads/community/2010/09/ ...
- no module named staticfiles
原地址:http://blog.sina.com.cn/s/blog_77500e110100umms.html 今天想试一下django的Uploadify,找了个例子,运行时出错:myprojec ...
- 【mysql5.6】数据类型
一.数值类型 int(5) 表示显示宽度为5,默认为显示宽度11. zerofill 属性:用0填充前面空位 auto_increment 属性:只用于整数.一般从1开始,每行增加1.一个表最多只能有 ...
- iOS打电话,发短信,发邮件,打开网址
//调用自带mail [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://admin@hzl ...
- C# 中Newtonsoft.Json的安装和使用
官网参考:http://json.codeplex.com/ 在程序包管理控制台,键入NuGet命令 install-package Newtonsoft.Json 安装Newtonsoft.Js ...
- windows JDK 版本切换
windows JDK 版本切换1. HKEY_LOCAL_MACHINE“SOFTWARE“JavaSoft“Java Runtime Environment“CurrentVersion, 把这个 ...
- 解决xshell连接linux总是异常断开的问题?
找到etc下面的profile文件,可以看到有下面一行: TMOUT=;export TMOUT 注释掉即可解决. #TMOUT=;export TMOUT