POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集
POJ 2236 Wireless Network
http://poj.org/problem?id=2236
题目大意:
给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y均能C通信,则x和y可以通信。现在给出若干个操作,
O p 代表修复编号为p的电脑
S p q代表询问p和q是不是能通信。
思路:
并查集即可。。
如果修复了一台电脑,则把与它相连距离不超过d的且修复了的放在一个集合里面。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=1000+10;
int fa[MAXN],n,d;
bool canuse[MAXN];
int find(int cur)
{
return fa[cur]<0? cur: fa[cur]=find(fa[cur]);
}
struct computers
{
int x,y;
}a[MAXN];
bool ok(int x1,int y1,int x2,int y2)
{
return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2) <= d*d;
}
int main()
{
memset(fa,-1,sizeof(fa));
memset(canuse,0,sizeof(canuse)); scanf("%d%d",&n,&d);
for(int i=1;i<=n;i++)
scanf("%d%d",&a[i].x,&a[i].y); char cmd[10];
int p,q;
while(~scanf("%s",cmd))
{
if(cmd[0]=='O')
{
scanf("%d",&p);
int root=find(p);
for(int i=1;i<=n;i++)
{
if(i==p) continue;
if(ok(a[i].x,a[i].y,a[p].x,a[p].y) && canuse[i])
{
int root1=find(i);
int root2=find(p);
if(root1!=root2)
fa[root1]=root2;
}
}
canuse[p]=true;
}
else
{
scanf("%d%d",&p,&q);
int rootp=find(p);
int rootq=find(q);
if(rootp==rootq)
printf("SUCCESS\n");
else
printf("FAIL\n");
}
}
return 0;
}
POJ 1703 Find them, Catch them
http://poj.org/problem?id=1703
题目大意:
xx城市有两个帮派,给你m条信息,D a b表示a和b不在一个帮派里。A a b时要求输出a和b是不是在一个帮派里。(在/不在/不确定)
思路:
和这题POJ 1182 食物链 并查集差不多。
划分为两个集合,给出D a b 说明a和b不在一个集合,那么就合并a和b+n,b和a+n。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=200000+10;
int fa[MAXN];
int find(int cur)
{
return cur==fa[cur]? cur: fa[cur]=find(fa[cur]);
}
void union_set(int a,int b)
{
int root_a=find(a);
int root_b=find(b);
if(root_a==root_b)
return;
fa[root_a]=root_b;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
int num=n*2;
for(int i=1;i<=num;i++)
fa[i]=i; char cmd[5];
int a,b;
for(int i=0;i<m;i++)
{
scanf("%s %d%d",cmd,&a,&b);
if(cmd[0]=='A')
{
if(find(a)==find(b))
printf("In the same gang.\n");
else if(find(a)==find(b+n) || find(b)==find(a+n))
printf("In different gangs.\n");
else
printf("Not sure yet.\n");
}
else
{
union_set(a,b+n);
union_set(a+n,b);
}
}
}
return 0;
}
POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集的更多相关文章
- [并查集] 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 ...
- poj 2236 Wireless Network (并查集)
链接:http://poj.org/problem?id=2236 题意: 有一个计算机网络,n台计算机全部坏了,给你两种操作: 1.O x 修复第x台计算机 2.S x,y 判断两台计算机是否联通 ...
- POJ 2236 Wireless Network(并查集)
传送门 Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 24513 Accepted ...
- POJ 2236 Wireless Network (并查集)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 18066 Accepted: 761 ...
- POJ 2236 Wireless Network (并查集)
Wireless Network 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/A Description An earthqu ...
- poj 2236 Wireless Network 【并查集】
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 16832 Accepted: 706 ...
- POJ 2236 Wireless Network [并查集+几何坐标 ]
An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wi ...
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题五 并查集 POJ 2236 Wireless Network
题意: 一次地震震坏了所有网点 现在开始修复它们 有N个点 距离为d的网点可以进行通信 O p 代表p点已经修复 S p q 代表询问p q之间是否能够通信 思路: 基础并查集 每次修复一个点重新 ...
随机推荐
- ArcGIS Engine 9.3启动程序报错
报错1: 没有注册类 (异常来自 HRESULT:0x80040154 解决办法: 由于是X64系统,将项目的生成目标该成X86就解决了. 报错2: 正试图在 OS 加载程序锁内执行托管代码.不要尝试 ...
- java.util.ConcurrentModificationException 异常解决的方法及原理
近期在修程序的bug,发现后台抛出下面异常: Exception in thread "main" java.util.ConcurrentModificationExceptio ...
- HBase写入操作卡住长时间不返回的原因分析
本文出处:http://blog.csdn.net/chaijunkun/article/details/44238163,转载请注明. 由于本人不定期会整理相关博文,会对相应内容作出完好.因此强烈建 ...
- Codefroces Round #429Div2 (A,B,C)
A. Generous Kefa time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- enable&&builtin---shell内部命令
用enable命令显示所有激活的内部命令: [root@localhost ~]# enable -a builtin命令用于执行指定的shell内部命令,并返回内部命令的返回值 [root@xiao ...
- WPF通用管理框架 项目客户端基础结构介绍
介绍 首先, 粗糙的展示一下目前的结构设计理念, 因为这几天一直在忙于工作, 所以跟进有些缓慢, 整体的设计是支持多种服务模式.目前只针对MSSQL做数据库接口, ORM选型则用的是微软的EF(PS: ...
- JavaWeb学习笔记:Tomcat
Tomcat 开源的 Servlet 容器. 部署并启动 tomcat server. 解压 apache-tomcat-6.0.16.zip 到一个非中文文件夹下. 配置一个环境变量. java_h ...
- Extjs, 使用GridPanel出现 Layout run failed
当GridPanel被加入到容器,且容器的layout为vbox时候, 会出现 Layout run failed 后者GridPanel的尺寸没有撑满父容器 网上找到的解决的方法是.要给父容器设置一 ...
- [NowCoder]牛客OI周赛1 题解
A.分组 首先,认识的人不超过3个,因此不存在无解的方案 考虑直接构造,先把所有点设为1,顺序扫一遍把有问题的点加入队列 每次取队头,将其颜色取反,再更新有问题的点 复杂度:考虑到每个点不会操作2次, ...
- spring的BeanWrapper类的原理和使用方法
转自:http://blog.sina.com.cn/s/blog_79ae79b30100t4hh.html 如果动态设置一个对象属性,可以借助Java的Reflection机制完成: Class ...