poj2236 基础并查集
题目链接:http://poj.org/problem?id=2236
题目大意:城市网络由n台电脑组成,因地震全部瘫痪,现在进行修复,规定距离小于等于d的电脑修复之后是可以直接相连
进行若干操作,O a,修复编号为a的电脑,S a,b 询问a,b电脑能否联系
思路分析:并查集,只是和并条件变了,首先要已经被修复(vis数组)其次距离要小于d,并查集搞完之后
询问的时候只需要看他们是不是有着相同的根节点即可。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn=+;
struct node
{
double x;
double y;
};
node com[maxn];
int fa[maxn];
bool vis[maxn];
int n,d;
double dis(int i,int j)
{
return sqrt((com[i].x-com[j].x)*(com[i].x-com[j].x)+(com[i].y-com[j].y)*(com[i].y-com[j].y));
}
int root(int x)
{
return (x==fa[x])?x:fa[x]=root(fa[x]);
}
void merge(int x,int y)
{
int fx=root(x);
int fy=root(y);
if(fx==fy) return;
fa[fx]=fy;
}
int main()
{
char s[];
int a,b;
memset(vis,false,sizeof(vis));
scanf("%d%d",&n,&d);
for(int i=;i<=n;i++)
{
scanf("%lf%lf",&com[i].x,&com[i].y);
fa[i]=i;
}
while(scanf("%s",s)!=EOF)
{
if(s[]=='O')
{
scanf("%d",&a);
vis[a]=true;
for(int i=;i<=n;i++)
{
if(dis(a,i)<=d&&vis[i]) merge(i,a);
}
}
if(s[]=='S')
{
scanf("%d%d",&a,&b);
if(root(a)==root(b))
printf("SUCCESS\n");
else printf("FAIL\n");
}
}
}
poj2236 基础并查集的更多相关文章
- hdu 1829 基础并查集,查同性恋
A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)
http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...
- 基础并查集poj2236
An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wi ...
- HDU4496 D-City【基础并查集】
Problem Description Luxer is a really bad guy. He destroys everything he met. One day Luxer went to ...
- AOJ 2170 Marked Ancestor (基础并查集)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=45522 给定一棵树的n个节点,每个节点标号在1到n之间,1是树的根节点,有如 ...
- POJ-2236.WireleseNetwork.(并查集)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 43199 Accepted: 178 ...
- CodeForces - 827A:String Reconstruction (基础并查集)
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun ...
- hdu1325 Is It A Tree? 基础并查集
#include <stdio.h> #include <string.h> ], g[]; int find(int x) //并查集的查找,找到共同的父亲 { if (f[ ...
- HDU1213How Many Tables(基础并查集)
HDU1213How Many Tables Problem Description Today is Ignatius' birthday. He invites a lot of friends. ...
随机推荐
- WM_CLOSE、WM_DESTROY、WM_QUIT的区别(询问,销毁窗口,退出进程,都不是一回事)
1.发送消息SendMessage.PostMessage PostMessage将消息放入消息队列后马上返回,而SendMessage直到窗口过程处理完消息后才返回 2.三个消息的区别 WM_CLO ...
- 怎么给qt程序添加版本信息
windows下的可执行文件的属性中有版本这个信息,她含有版本信息,描述,版权等等.对于qt的程序,要含有这样的信息,该怎么办呢?那就如下操作吧:新建***.rc文件,在rc文件填入下的信息 #if ...
- Delphi应用程序的调试(十)调试器选项(在IDE中不要使用异常)
可在两个级别上设置调试选项:工程级和环境级.在前面的讲解中讲解了工程级调试选项,通过主菜单[Project | Options…]打开如下对话框: 可在Debugger Options对话框中设置全局 ...
- Android 介绍spydroid每个包的大体功能
看了接近一周的spydroid源代码,对spydroid这个开源项目有了一定的认识.也许有些理解不一定正确,给后来者一点启示.也是自己对rtsp协议,rtp协议的总结. 在windows下,如果安装了 ...
- POJ 1704 Georgia and Bob (Nim游戏变形)
题目:http://poj.org/problem?id=1704 思路:Nim游戏策略,做如下转换,如果N是偶数,则两两配对,将两个数之间的格子数(距离)看做成这一堆石头的数量. 如果N是奇数,则将 ...
- POJ3295 Tautology(枚举)
题目链接. 分析: 最多有五个变量,所以枚举所有的真假值,从后向前借助于栈验证是否为永真式. #include <iostream> #include <cstring> #i ...
- BZOJ2718: [Violet 4]毕业旅行
2718: [Violet 4]毕业旅行 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 229 Solved: 126[Submit][Status ...
- 【转】Android进阶2之Activity之间数据交流(onActivityResult的用法)----不错
原文网址:http://blog.csdn.net/sjf0115/article/details/7387467 主要功能: 在一个主界面(主Activity)上能连接往许多不同子功能模块(子Act ...
- TCP协议下Socket的基础编程类型
套接字的基本操作有: 创建(socket).命名(bind).侦听(listen).连接(accept).关闭(shutdown).发送(send).接受(recv). 下面逐个分析: 一.创建(so ...
- [Design Pattern] Service Locator Pattern 简单案例
Service Locator Pattern,即服务定位模式,用于定位不同的服务.考虑到 InitialContext::lookup 的成本比较高,提供了 Cache 类缓存以定位到的服务. 代码 ...