POJ 2236 (簡單並查集) Wireless Network

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   ar   for   art   

題意:

有n個電腦壞掉了,分別給出他們的座標

有兩種操作,可以O x表示修好第x台電腦,可以 S x y表示x y是否連通

兩台電腦的距離不超過d便可連通,兩台電腦是連通的可以直接連通也可以間接通過第三台電腦連通

思路:

每次修好一台電腦都和前面已經修好的電腦比較一下如果距離小於d而且不在同一網路,便合并在一起即可

 

 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std; 6  7 const int maxn = 1000 + 10; 8 struct Pos 9 {10     int x, y;11 }pos[maxn];12 13 int p[maxn], o[maxn];14 15 int Find(int a)16 {17     return p[a] == a ? a : p[a] = Find(p[a]);18 }19 20 void Union(int a, int b)21 {22     int pa = Find(a);23     int pb = Find(b);24     if(pa != pb)25         p[pa] = pb;26 }27 28 int main(void)29 {30     #ifdef LOCAL31         freopen("2236in.txt", "r", stdin);32     #endif33 34     int n, d, a, b, cnt = 0;35     char cmd[10];36     scanf("%d%d", &n, &d);37     for(int i = 0; i <= n; ++i)    p[i] = i;38     memset(o, false, sizeof(o));39     for(int i = 1; i <= n; ++i)40         scanf("%d%d", &pos[i].x, &pos[i].y);41     while(scanf("%s", cmd) != EOF)42     {43         if(cmd[0] == ‘O‘)44         {45             scanf("%d", &a);46             o[cnt++] = a;47             for(int i = 0; i < cnt - 1; ++i)48             {49                 if((pos[a].x-pos[o[i]].x)*(pos[a].x-pos[o[i]].x) + (pos[a].y-pos[o[i]].y)*(pos[a].y-pos[o[i]].y) <= d * d)50                 {51                     Union(a, o[i]);52                 }53             }54         }55         else56         {57             scanf("%d%d", &a, &b);58             if(Find(a) == Find(b))59                 puts("SUCCESS");60             else61                 puts("FAIL");62         }63     }64 65     return 0;66 }
代碼君

 

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.