POJ2236 Wireless Network

解题思路:简单并查集,注意时间限制是10000MS,每次进行O操作之后,

      进行一次for循环,进行相关调整。同时注意输入输出格式,见代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<cmath>
 5 using namespace std;
 6 const int maxn = 1005;
 7 int father[maxn], vis[maxn];
 8 
 9 struct node{
10     double x, y; //此处用double
11 }p[maxn];
12 
13 int Find(int x)
14 {
15     return father[x] == x ? x : father[x] = Find(father[x]);
16 }
17 
18 void Union(int x, int y)
19 {
20     int rootx = Find(x), rooty = Find(y);
21     if(rootx != rooty) father[rootx] = rooty;
22     return ;
23 }
24 
25 int main()
26 {
27     int n, d, k, a, b;
28     char ch;
29     scanf("%d %d", &n, &d);
30     memset(vis, 0, sizeof(vis));
31     for(int i = 1; i <= n; i++) father[i] = i; 
32     for(int i = 1; i <= n; i++) scanf("%lf %lf", &p[i].x, &p[i].y);
33     while(~scanf(" %c", &ch))
34     {
35         if(ch == 'O')
36         {
37             scanf("%d", &k);
38             vis[k] = 1;
39             for(int i = 1; i <= n; i++)
40             {
41                 if(!vis[i]) continue; //必须是已经修过的
42                 //两点距离小于等于d,则可以合并
43                 if(sqrt((p[i].x-p[k].x)*(p[i].x-p[k].x)+(p[i].y-p[k].y)*(p[i].y-p[k].y)) <= d)
44                 Union(i, k);
45             }
46         }
47         else
48         {
49             scanf("%d %d", &a, &b);
50             //根节点相同,则表明可以连接
51             if(Find(a) == Find(b)) printf("SUCCESS
");
52             else printf("FAIL
");
53         }
54     }
55     return 0;
56 }
View Code
原文地址:https://www.cnblogs.com/loveprincess/p/4892277.html