POJ-2236 Wireless Network

来源:互联网 发布:淘宝网管家 编辑:程序博客网 时间:2024/06/02 19:35

题目链接:http://poj.org/problem?id=2236

题目大意:

给你N台电脑,从1-N。一个数字,表示两台计算机的最大通信距离,超过这个距离就无法进行通信。然后分别告诉这些电脑的坐标,接下来有两种操作,第一种O表示这点电脑修好,第二种S,表示测试这两台电脑能不能进行正常的通信


解题思路:

并查集的简单应用,对每次修好的电脑对其它已经修好的电脑遍历,如果距离小于等于最大通信距离就将他们合并。之后判断2台电脑是不是一个集合中就KO了

PS:这道题的输入也要小心,就是因为输入问题,wrong了好几次,改成cin就过了,看来字符的输入一定要特别小心。。。。抓狂

代码如下:

#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>using namespace std;#define N 1110int d;bool use[N];struct node{int pre;int x, y;}p[N];int find(int x){return x == p[x].pre ? x : find(p[x].pre);}void join(const node p1, const node p2){int root1, root2;root1 = find(p1.pre);root2 = find(p2.pre);if(root1 != root2)if((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y) <= d * d)p[root2].pre = root1;}int main(){//freopen("Input.txt", "r", stdin);int num;char ope;int ok;int from, to;scanf("%d%d", &num, &d);for(int i = 1; i <= num; ++i)p[i].pre = i;memset(use, false, sizeof(use));for(int i = 1; i <= num; ++i)scanf("%d%d", &p[i].x, &p[i].y);while(scanf("\n%c", &ope) != EOF){if(ope == 'O'){scanf("%d", &ok);use[ok] = true;for(int i = 1; i <= num; ++i)if(use[i] && i != ok)join(p[i], p[ok]);}else{scanf("%d%d", &from, &to);if(find(from) == find(to))printf("SUCCESS\n");elseprintf("FAIL\n");}}return 0;}