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之间是否能够通信 思路: 基础并查集 每次修复一个点重新 ...
随机推荐
- 关于mysql数据库在输入password后,滴的一声直接退出界面的解决的方法(具体办法)
前一阵子.因为敲代码要用到数据库,便在本子上下载了一个,却出现非常多小问题(自己的台式机却没有该问题,可能是本人的本子太渣了吧),纠结了好一阵,回头想想.发现问题,分析问题,解决这个问题,不就是我们的 ...
- session timer(一)
功能介绍 SIP并没有为所建立的会话定义存活机制. 虽然用户代理能够通过会话特定的机制推断会话是否超时,可是代理server却做不到这点. 如此一来.代理server有时会无法推断会话是否还是活动的. ...
- .Net强类型视图
1.控制器 Controllers/StoreController.cs using System; using System.Collections.Generic; using System.Li ...
- BZOJ 1231 状压DP
思路: f[i][j] i表示集合的组成 j表示选最后一个数 f[i][j]表示能选的方案数 f[i|(1<< k)][k]+=f[i][j]; k不属于i j属于i且符合题意 最后Σf[ ...
- spring源码分析之@Conditional
根源在AnnotationConfigApplicationContext和AnnotationConfigWebApplicationContext,以AnnotationConfigApplica ...
- C# 开发 —— 数组类对象接口
数组类型是从抽象基类 Array 派生的引用类型,通过new运算符创建数组并将数组元素初始化为他们的默认值 一维数组 type[] arrayname; 数组的长度不是声明的一部分,而且数组必须在访问 ...
- jq--图片懒加载
html 1.给图片不给真真意义上的src属性路径,可通过我们自己想要添加时改变它的属性路径即可. 2.要获取浏览器中三种高度. $(window).height();//屏幕高度 $(window) ...
- BZOJ1814: Ural 1519 Formula 1(插头Dp)
Description Regardless of the fact, that Vologda could not get rights to hold the Winter Olympic gam ...
- 【Educational Codeforces Round 36 D】 Almost Acyclic Graph
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 找到任意一个环. 然后枚举删掉其中的某一条边即可. (因为肯定要删掉这个环的,那么方法自然就是删掉其中的某一条边 (其它环,如果都包 ...
- [置顶]
Docker学习总结(2)——Docker实战之入门以及Dockerfile(二)
csphere/php-fpm:5.4 # cd docker-training/php-fpm/ # ls Dockerfile nginx_nginx.conf supervisor_nginx. ...