Time Limit: 10000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
Description
Input
Output
Sample Input
4 1 0 1 0 2 0 3 0 4 O 1 O 2 O 4 S 1 4 O 3 S 1 4
Sample Output
FAIL SUCCESS
简单并查集,输入n电脑数量和d两台电脑的最大通信距离,下面n行是1-n台电脑的位置坐标,接下来输入到文件结束是操作O a,表示修好第a台电脑,S a b查询这两台电脑是否可以通信。每次修好一台电脑遍历存在的电脑,把通信范围内的电脑并入一个集合就好了。注意输入到文件结束。
#include<iostream> #include<cstring> #include<cstdio> using namespace std; struct Point { int x, y; }; const int MAXN = 1000 + 100; int parent[MAXN]; int n, d; Point p[MAXN]; bool isrep[MAXN]; void make_set() { for (int i = 0; i <= n; i++) { parent[i] = i; isrep[i] = false; } } int find_set(int t) { if (parent[t] == t) return t; else return parent[t] = find_set(parent[t]); } void union_set(int a, int b) { int t1 = find_set(a); int t2 = find_set(b); if (t1 != t2) { parent[t2] = t1; } } bool isIn(Point a, Point b) { int dd = (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y); if (dd <= d*d) return true; return false; } int main() { scanf("%d%d", &n, &d); for (int i = 1; i <= n; i++) { scanf("%d%d", &p[i].x, &p[i].y); } char op; int a, b; make_set(); while(cin>>op) { if (op == 'S') { scanf("%d%d", &a, &b); if (find_set(parent[a]) == find_set(parent[b])) printf("SUCCESS\n"); else printf("FAIL\n"); } else { scanf("%d", &a); for (int j = 1; j <= n; j++) { if (isrep[j]) { if (isIn(p[a], p[j])) union_set(a, j); } } isrep[a] = true; } } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ - 2236 Wireless Network(简单并查集)
原文地址:http://blog.csdn.net/qq_18738333/article/details/48003345