POJ 2236 Wireless Network (並查集)

來源:互聯網
上載者:User

標籤:連通   gets   ott   題意   getc   max   pos   eof   style   

題意:

有n台損壞的電腦,現要將其逐台修複,且使其相互恢複通訊功能。若兩台電腦能相互連信,則有兩種情況,一是他們之間的距離小於d,二是他們可以藉助都可到達的第三台已修複的電腦。給出所有電腦的座標位置,對其進行兩種可能的操作,O x表示修複第x台,S x y表示判斷x y之間能否通訊,若能輸出SUCCESS,否則輸出FALL。

思路:

用並查集來儲存電腦互相的連通情況。
每次修好電腦後,將它可以通訊的電腦(距離滿足且已修好)與它進行連通。

#include <cstdio>  #include <cstring>    #include <string>  #include <iostream>  using namespace std;const int maxn = 1000 + 5;struct node{int x, y;}pos[maxn];int p[maxn], vis[maxn];int find(int x) {return p[x] == x ? x : p[x] = find(p[x]);}void unionset(int x, int y) {int px = find(x), py = find(y);if (px != py) p[px] = py;}int main() {int n, d;scanf("%d%d", &n, &d);memset(vis, 0, sizeof(vis));for (int i = 1; i <= n; ++i) p[i] = i;for (int i = 1; i <= n; ++i) scanf("%d%d", &pos[i].x, &pos[i].y);getchar();char s[20];while (fgets(s, sizeof(s), stdin) != NULL) {char op;int a, b;if (s[0] == ‘S‘) {sscanf(s, "%c %d %d", &op, &a, &b);int rx = find(a), ry = find(b);if (rx == ry) printf("SUCCESS\n");else printf("FAIL\n");}else {sscanf(s, "%c %d", &op, &a);vis[a] = 1;int x = pos[a].x, y = pos[a].y;for (int i = 1; i <= n; ++i) {if (a == i || !vis[i]) continue;int x1 = pos[i].x, y1 = pos[i].y;if ((x - x1)*(x - x1) + (y - y1)*(y - y1) <= d*d) {unionset(a, i);}}}}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.