poj-2236-Wireless Network

来源:互联网 发布:淘宝代理需要多少钱 编辑:程序博客网 时间:2024/05/18 04:00

并查集简单题 题意表面是1000*20000的数量级 其实最坏只需要(1+1000)*1000/2次合并就行了。。。

#include<cstdio>int n,d;char order[10];struct node{    int x,y;}a[2000];int fa[2000],am[2000],st[2000];void init() {    for(int i=1;i<=n;i++)      {        fa[i] = -1;        am[i] = 0;      }}int toGetFarther(int i){    while(!(fa[i]==i||fa[i]==-1))     i = fa[i];     return i;}void unon(int x,int y){    int fax = toGetFarther(x),fay = toGetFarther(y);    if(fax!=fay)    {        if(am[fax]>am[fay])        {            fa[fay] = fax;            am[fax]+=am[fay];        }        else        {            fa[fax] = fay;            am[fay] += am[fax];        }    }}int main(){    scanf("%d%d",&n,&d);    int l1 = 0;    init();    for(int i=1;i<=n;i++)      scanf("%d%d",&a[i].x,&a[i].y);    while(scanf("%s",order)!=EOF)    {        if(order[0]=='O')        {            int to;            scanf("%d",&to);            if(fa[to]!=-1)continue;//如果已经修过就没必要再修了。。。            else{                fa[to] = to;                am[to] = 1;                for(int i=0;i<l1;i++)                 if((a[st[i]].x-a[to].x)*(a[st[i]].x-a[to].x)+(a[st[i]].y-a[to].y)*(a[st[i]].y-a[to].y)<=d*d)                     unon(st[i],to);                 st[l1] = to;                 l1++;            }        }        else        {            int st,en;            scanf("%d%d",&st,&en);            int stf = toGetFarther(st);            int enf = toGetFarther(en);            if(stf==enf&&stf!=-1)printf("SUCCESS\n");            else printf("FAIL\n");        }    }    return 0;}
0 0
原创粉丝点击