Wireless Network(POJ

来源:互联网 发布:富士 打印软件 编辑:程序博客网 时间:2024/06/08 05:03

一、题目大意

地震导致电脑之间失联,现在开始修复工作。若电脑A与电脑B之间的距离小于D,则可以修复。若A与B可互联,B与C可互联,则A与C可互联。

"O p" 代表电脑p已经被修好。

"S p q"代表查询电脑p和电脑q是否可以互联。


二、题目分析

比较基础的并查集。


三、附代码

#include<iostream>#include<cstdio>#include<algorithm>#include<stack>#include<queue>#include<cstring>#include<string>#include<set>#include<cmath>#include<map>#include<sstream>using namespace std;#define inf 0x3f3f3f3ftypedef long long LL;const int maxn = 1000 + 8;struct Node{    int x,y;    int par;    bool mark;//表示是否修好}node[maxn];int n,d,x,y;bool Judge(int a,int b){    double disx = (node[a].x - node[b].x) * (node[a].x - node[b].x);    double disy = (node[a].y - node[b].y) * (node[a].y - node[b].y);    double dis = sqrt(disx + disy);    return dis <= d;}int Find(int a){    while(a != node[a].par) a = node[a].par;    return a;}void Unite(int x,int y){    x = Find(x);    y = Find(y);    if(x == y) return;    node[y].par = x;}bool Same(int a,int b){    return Find(a) == Find(b);}int main(){    cin >> n >> d;    for(int i = 1; i <= n; ++i){        cin >> node[i].x >> node[i].y;        node[i].par = i;        node[i].mark = false;    }    string s;    while(cin >> s)    {        if(s == "O"){            cin >> x;            node[x].mark = true;            for(int i = 1; i <= n; ++i){                if(i != x && Judge(i,x) && node[i].mark){                   // cout << i << " " << x << endl;                    Unite(i,x);                }            }        }else if(s == "S"){            cin >> x >> y;            if(Same(x,y)){                cout << "SUCCESS" << endl;            }else{                cout << "FAIL" << endl;            }        }    }    return 0;}/*4 10 10 20 30 4O 1O 2O 4S 1 4O 3S 1 4*/




原创粉丝点击