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
题意:给你n个坏的电脑的坐标,如果两个修好的电脑距离不大于d,他们之间连通。两台电脑可以通过第三台电脑间接连通。给出若干次询问,一种是修哪台电脑,一种是询问两台电脑是否连通。
题解:典型的并查集,合并时我也没有加rank数组优化,可以自己加一下,不过10s时间绰绰有余了。
#include <iostream> #include <sstream> #include <fstream> #include <string> #include <map> #include <vector> #include <list> #include <set> #include <stack> #include <queue> #include <deque> #include <algorithm> #include <functional> #include <iomanip> #include <limits> #include <new> #include <utility> #include <iterator> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cmath> #include <ctime> using namespace std; const int maxn = 1010; int f[maxn]; int px[maxn], py[maxn]; int vis[maxn]; int n, d; int Find(int x) { return x == f[x] ? x : (f[x] = Find(f[x])); } void join(int x, int y) { int fx = Find(x), fy = Find(y); f[fx] = fy; } int dis(int x, int y) { return (px[x]-px[y])*(px[x]-px[y])+(py[x]-py[y])*(py[x]-py[y]); } int main() { cin >> n >> d; for (int i = 1; i <= n; ++i) { scanf("%d%d", &px[i], &py[i]); f[i] = i; } memset(vis, 0, sizeof(vis)); char op[5]; while (~scanf("%s", op)) if (op[0] == 'O') { int p; scanf("%d", &p); vis[p] = 1; for (int i = 1; i <= n; ++i) if (i != p && vis[i] && dis(i, p) <= d*d) join(i, p); } else { int x, y; scanf("%d%d", &x, &y); int fx = Find(x), fy = Find(y); if (fx == fy) printf("SUCCESS\n"); else printf("FAIL\n"); } return 0; }
版权声明:本文为博主原创文章,转载请注明出处。
原文地址:http://blog.csdn.net/god_weiyang/article/details/47753935