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之间是否能够通信 思路: 基础并查集 每次修复一个点重新 ...
随机推荐
- Java Web学习总结(9)——servlet和Jsp生命周期解读
一.servlet的工作工程 Servlet是运行在Servlet容器(有时候也叫Servlet引擎,是web服务器和应用程序服务器的一部分,用于在发送的请求和响应之上提供网络服务,解码基于MIME的 ...
- Tomcat线程池与NIO配置
每个web客户端请求对于服务器端来说就一个单独的线程,客户端的请求数量增多将会导致线程数就上去了,CPU就忙着跟线程切换. 而NIO则是使用单线程(单个CPU)或者只使用少量的多线程(多CPU)来接受 ...
- WMI获取进程CPU占用率
Monitor % Process CPU Usage (specific process) http://www.tek-tips.com/viewthread.cfm?qid=395765 for ...
- thinkphp3.2.3 隐藏url上home模块以及index.php文件
1.去掉Home index.php 添加如下代码 define('BIND_MODULE', 'Home'); 这时就隐藏了url中的Home 2.去掉index.php thinkphp3.2.3 ...
- CODEVS——T 4189 字典
http://codevs.cn/problem/4189/ 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 题解 查看运行结果 题目描述 Des ...
- 洛谷 P1324 矩形分割
P1324 矩形分割 题目描述 出于某些方面的需求,我们要把一块N×M的木板切成一个个1×1的小方块. 对于一块木板,我们只能从某条横线或者某条竖线(要在方格线上),而且这木板是不均匀的,从不同的线切 ...
- new方法的实现原理
// // main.m // 04-new方法的实现原理 #import <Foundation/Foundation.h> #import "Person.h" # ...
- RvmTranslator6.5 is released
RvmTranslator6.5 is released eryar@163.com RvmTranslator can translate the RVM file exported by AVEV ...
- jQuery Easy UI ProgressBar(进度条)组件
ProgressBar(进度条)组件,这个还是挺好玩的.我们在自己做点什么的时候常常能用到,比方上传下载文件.导入导出文档啊.加载网页等等. 应用场景非常多,使用起来还非常easy. 演示样例: &l ...
- Unity中uGUI的控件事件穿透逻辑
1.正常来说Image和Text是会拦截点击事件的,假设加入EventTrigger的话,就能够响应相应的交互事件. 2.假设Image和Text是一个Button的子控件.那么尽管其会显示在Butt ...