poj 2236 Wireless Network (並查集)

來源:互聯網
上載者:User

標籤:stream   for   class   void   bool   wireless   style   ons   can   

連結:http://poj.org/problem?id=2236

題意:

有一個電腦網路,n台電腦全部壞了,給你兩種操作:

1.O x 修複第x台電腦

2.S x,y 判斷兩台電腦是否聯通

聯通的條件: 兩台都修複好了,求距離小於d

思路:

資料很小,我們可以沒修複一台就直接枚舉已經修複的電腦找到距離小於d的,放到一個並查集裡,查詢的話直接查詢是否再同一個並查集裡就好了

 

實現代碼:

//while(scanf("\n%c", &ope) != EOF)#include<iostream>#include<cstdio>using namespace std;const int M = 1e4+10;struct node{    int x,y,id;}a[M],b[M];int n,d;int f[M];int find(int x){    if(x == f[x]) return x;    return f[x] = find(f[x]);}void mix(int x,int y){    int fx = find(x);    int fy = find(y);    if(fx != fy) f[fx] = fy;}bool check(node a,node b){    double num = (a.x - b.x)*(a.x - b.x)+(a.y - b.y)*(a.y - b.y);    double num1 = d*d*1.0;    if(num <= num1) return 1;    return 0;}int main(){    char op;int k,l,r,cnt = 0;    scanf("%d%d",&n,&d);    for(int i = 1;i <= n;i ++)        scanf("%d%d",&a[i].x,&a[i].y),a[i].id = i;    for(int i = 0;i <= n;i ++)        b[i].x = 0,b[i].y=0,b[i].id = 0,f[i]= i;    while(scanf("\n%c", &op) != EOF){        if(op == ‘O‘){            scanf("%d",&k);            for(int i = 1;i <= cnt;i ++)                if(check(b[i],a[k]))                    mix(b[i].id,k);            b[++cnt].x = a[k].x;b[cnt].y = a[k].y,b[cnt].id = a[k].id;        }        else{            scanf("%d%d",&l,&r);            if(find(l) == find(r)) printf("SUCCESS\n");            else printf("FAIL\n");        }    }}

 

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.