并查集水题

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
int n,m,t;
struct node
{
int x,y;
}A[];
int rank[],pre[];
int dist(node A,node B)
{
return (A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y);
}
void init(int n)
{
for(int i=;i<=n;i++)
{
rank[i]=;
pre[i]=i;
}
}
int find(int x)
{
if(pre[x]!=x) pre[x]=find(pre[x]);
return pre[x];
}
void Union(int x,int y)
{
x=find(x);
y=find(y);
if(x==y) return;
if(rank[x]>=rank[y])
{
pre[y]=x;
rank[x]+=rank[y];
}
else
{
pre[x]=y;
rank[y]+=rank[x];
}
}
int main()
{
int i,j,k;
//freopen("1.in","r",stdin);
scanf("%d",&n);
init(n);
scanf("%d",&k);
for(i=;i<=n;i++) scanf("%d%d",&A[i].x,&A[i].y);
char s[];
int aa[],tot=;
while(scanf("%s",s)!=EOF)
{
if(s[]=='O')
{
int q;
scanf("%d",&q);
for(i=;i<tot;i++)
{
if(dist(A[aa[i]],A[q])<=k*k)
{
// printf("%d %d\n",aa[i],q);
Union(aa[i],q);
}
}
aa[tot++]=q;
}
else
{
int p,q;
scanf("%d%d",&p,&q);
p=find(p);
q=find(q);
if(p==q) printf("SUCCESS\n");
else printf("FAIL\n");
}
}
return ;
}

poj 2236 并查集的更多相关文章

  1. Wireless Network POJ - 2236 (并查集)

    #include<iostream> #include<vector> #include<string> #include<cmath> #includ ...

  2. poj 1984 并查集

    题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 /* POJ 1984 并查集 */ #include <stdio.h& ...

  3. poj 1797(并查集)

    http://poj.org/problem?id=1797 题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路 ...

  4. POJ 2492 并查集扩展(判断同性恋问题)

    G - A Bug's Life Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  5. POJ 2492 并查集应用的扩展

    A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Descri ...

  6. POJ 3228 [并查集]

    题目链接:[http://poj.org/problem?id=3228] 题意:给出n个村庄,每个村庄有金矿和仓库,然后给出m条边连接着这个村子.问题是把所有的金矿都移动到仓库里所要经过的路径的最大 ...

  7. poj 1733 并查集+hashmap

    题意:题目:有一个长度 已知的01串,给出多个条件,[l,r]这个区间中1的个数是奇数还是偶数,问前几个是正确的,没有矛盾 链接:点我 解题思路:hash离散化+并查集 首先我们不考虑离散化:s[x] ...

  8. poj 3310(并查集判环,图的连通性,树上最长直径路径标记)

    题目链接:http://poj.org/problem?id=3310 思路:首先是判断图的连通性,以及是否有环存在,这里我们可以用并查集判断,然后就是找2次dfs找树上最长直径了,并且对树上最长直径 ...

  9. POJ 3657 并查集

    题意: 思路: 1.二分+线段树(但是会TLE 本地测没有任何问题,但是POJ上就是会挂--) 2.二分+并查集 我搞了一下午+一晚上才搞出来----..(多半时间是在查错) 首先 如果我们想知道这头 ...

随机推荐

  1. ArrayList与LinkedList区别

    ArrayList 采用的是数组形式来保存对象的,这种方式将对象放在连续的位置中,所以最大的缺点就是插入删除时非常麻烦LinkedList 采用的将对象存放在独立的空间中,而且在每个空间中还保存下一个 ...

  2. SQL injection

    SQL injection is a code injection technique, used to attack data-driven applications, in which malic ...

  3. php中mysql参数化查询

    $query = sprintf("SELECT * FROM Users where UserName='%s' and Password='%s'",mysql_real_es ...

  4. IIS负载均衡-Application Request Route详解第二篇:创建与配置Server Farm(转载)

    IIS负载均衡-Application Request Route详解第二篇:创建与配置Server Farm 自从本系列发布之后,收到了很多的朋友的回复!非常感谢,同时很多朋友问到了一些问题,有些问 ...

  5. B0BO TFS 安装指南(转载)

    TFS2008安装过几次,每次都遇到点麻烦,结合网上的一些经验总结一下: Windows SharePoint Services 安装 Windows SharePoint Services你有两个选 ...

  6. 保护隐私:清除cookie、禁用cookie确保安全【分享给身边的朋友吧】

    常在网上漂,隐私保不了.ytkah深有体会,某天搜索一个词,然后你就能在一些网站上看到这个词的相关广告,神奇吧?这就是你的浏览器cookie泄露了,或者更严重地说是你的隐私泄露了,可怕吧!搜索引擎通过 ...

  7. zoj.3865.Superbot(bfs + 多维dp)

    Superbot Time Limit: 2 Seconds      Memory Limit: 65536 KB Superbot is an interesting game which you ...

  8. Python列表基础

    ==========列表基础=========== 列表中的数据是可以被修改的.字典,元组,集合是不能被修改的. >>> li1=['3edf','dafdas'] >> ...

  9. android edittext 去边框

    EditText的background属性设置为@null就搞定了:android:background="@null" style属性倒是可加可不加 附原文:@SlumberMa ...

  10. Dialog类介绍

    Dialog类实现为一个简单的漂浮窗口,完全在Activity中创建.使用基本的Dialog类,你可以创建一个新的实例并设定标题和布局,如下所示: Dialog d = new Dialog(MyAc ...