POJ 2236 Wireless Network (並查集)

來源:互聯網
上載者:User

標籤:wireless network   poj 2236   並查集   an earthquake takes   poj   

題目連結:請戳這裡。

思路及大意:給你n個電腦的座標,然後是兩種操作,‘o‘是修複某台電腦,’s‘是詢問p,q兩台電腦是否能夠串連。

計算每次修複好的電腦與之前修複好的電腦之間的距離,若在d內,則將他們放入一個集合中;

若p,q兩台電腦在一個集合中,則表示他們可以串連。

#include<cstdio>#include<cstring>#include<algorithm>#define N 1000+10using namespace std;int x[N],y[N],v[N];int dist(int i,int j){    return (x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);}int f[N];void Init(int n){    for(int i=0;i<n;i++)        f[i]=i;}int Find(int x){    return x==f[x]?x:f[x]=Find(f[x]);}void Merge(int a,int b){    int ra=Find(a),rb=Find(b);    if(ra==rb){        return ;    }else{        f[rb]=ra;    }}int main(){    int n,d;    scanf("%d %d",&n,&d);    for(int i=1;i<=n;i++)        scanf("%d %d",&x[i],&y[i]);    Init(n);    memset(v,0,sizeof(v));    char str[5];    while(scanf("%s",str)!=EOF)    {        if(str[0]=='O')        {            int k;            scanf("%d",&k);            v[k]=1;            for(int i=1;i<=n;i++)            {                if(v[i] && dist(k,i)<=d*d)                    Merge(k,i);            }        }        else        {            int a,b;            scanf("%d %d",&a,&b);            int ra=Find(a),rb=Find(b);            if(ra==rb) puts("SUCCESS");            else puts("FAIL");        }    }    return 0;}


POJ 2236 Wireless Network (並查集)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.