1. 程式人生 > >POJ-2236-Wireless Network

POJ-2236-Wireless Network

cooper == fail 中轉 ret spa stream int istream

鏈接:https://vjudge.net/problem/POJ-2236#author=0

題意:

南亞發生了一次地震。ACM (Asia Cooperated Medical 亞洲聯合醫療隊) 已經為膝上型電腦搭建了一個無線網絡,但受到了一次不可預知的余震攻擊,因此網絡中的所有電腦都被破壞了。電腦被逐臺修復,網絡逐步恢復了工作。由於受到硬件的約束,每臺電腦只能與距離它不超過 d 米的其它電腦直接通信。但每臺電腦可被看作其它兩臺電腦的通信中轉點,也就是說,如果電腦 A 和電腦 B 可以直接通信,或存在一臺電腦 C 既可與 A 也可與 B 通信,那麽電腦 A 和電腦 B 之間就能夠通信。

在處理網絡修復的過程中,工作人員們在任何一個時刻,可以執行兩種操作:維修一臺電腦,或測試兩臺電腦是否能夠通信。請您找出全部的測試操作。

思路:

並查集,先處理輸入的結點。用vector保存每個結點可以直接連接的點。

每次修復,將查找結點的連接點,將修復過的找到父節點,並令其父親為當前修復的。

判斷聯通直接查找父節點即可。

代碼:

#include <iostream>
#include <memory.h>
#include <string>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
#include <math.h>
using namespace std;
const int MAXN = 10100;
int n,d;

struct Node
{
    double _x;
    double _y;
}node[MAXN];
vector<int> member[MAXN];
int Father[MAXN];
int Vis[MAXN];

double Get_Len(Node a,Node b)
{
    return sqrt((a._x-b._x)*(a._x-b._x) + (a._y-b._y)*(a._y-b._y));
}

int Get_F(int x)
{
    return Father[x] = Father[x] == x ? x:Get_F(Father[x]);
    if (Father[x] == x)
        return x;
    else
    {
        Father[x] = Get_F(Father[x]);
        return Father[x];
    }
}

int main()
{
    scanf("%d%d",&n,&d);
    for (int i = 1;i<=n;i++)
        scanf("%lf%lf",&node[i]._x,&node[i]._y);

    for (int i = 1;i<=n;i++)
    {
        Father[i] = i;
        for (int j = 1; j <= n; j++)
            if (i != j && Get_Len(node[i], node[j]) <= d)
                member[i].push_back(j);
    }
    //構造
    string s;
    while (cin >> s)
    {
        if (s[0] == ‘S‘)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            int ta = Get_F(a);
            int tb = Get_F(b);
            if (ta == tb)
                printf("SUCCESS\n");
            else
                printf("FAIL\n");
        }
        else
        {
            int a,b;
            scanf("%d",&a);
            Vis[a] = 1;
            for (int i = 0;i<member[a].size();i++)
            {
                if (Vis[member[a][i]])
                {
                    b = Get_F(member[a][i]);
                    Father[b] = a;
                }
            }
        }
    }

    return 0;
}

  

POJ-2236-Wireless Network