poj 2236 加点 然后判断某两点是否连通

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

修电脑就是把这点加入到图中,S 就是判断这两个结点在不在同一个集合里,也就是是否连通

Sample Input

4 1 //n d
0 1 // x y
0 2
0 3
0 4
O 1
O 2
O 4
S 1 4
O 3
S 1 4
Sample Output

FAIL
SUCCESS

 

 1 # include <iostream>
 2 # include <cstdio>
 3 # include <cstring>
 4 # include <algorithm>
 5 # include <cmath>
 6 # include <queue>
 7 # define LL long long
 8 using namespace std ;
 9 
10 const int MAXN=1010;
11 int F[MAXN] ;
12 int x[MAXN] ;
13 int y[MAXN] ;
14 int v[MAXN] ;
15 int n , d ;
16 int find(int x)//找x的祖先结点
17 {
18     if(F[x]==x) return x;
19     return F[x]=find(F[x]);
20 }
21 void bing(int u,int v)
22 {
23     int t1=find(u);
24     int t2=find(v);
25     if(t1!=t2) //这两个点不在一个集合里
26       F[t1]=t2; //合到一个集合里
27 }
28 
29 bool dist(int a , int b)
30 {
31     int t1 = x[a] - x[b] ;
32     int t2 = y[a] - y[b] ;
33     if(t1*t1 + t2*t2 <= d*d)
34         return 1 ;
35     else
36         return 0 ;
37 }
38 
39 int main()
40 {
41     //freopen("in.txt","r",stdin) ;
42     cin>>n>>d ;
43     int i ;
44     for (i = 1 ; i<= n ; i++)
45         cin>>x[i]>>y[i] ;
46     for (i = 1 ; i<= n ; i++)
47     {
48         F[i] = i ;
49         v[i] = 0 ;
50     }
51     char c ;
52     int t ;
53     while(cin>>c)
54     {
55         if (c == 'O')
56         {
57             cin>>t ;
58             v[t] = 1 ;
59             for (i = 1 ; i<= n ; i++)
60             {
61                 if (i != t && v[i] && dist(i,t))
62                     bing(i,t) ;
63             }
64         }
65         if (c == 'S')
66         {
67             int u , v ;
68             cin>>u>>v ;
69             int t1=find(u);
70             int t2=find(v);
71             if(t1!=t2) //不连通
72                 cout << "FAIL" << endl;
73             else
74                 cout << "SUCCESS" << endl;
75         }
76     }
77 
78 
79     return 0;
80 }
View Code

 

posted @ 2015-06-23 20:54  __Meng  阅读(486)  评论(0编辑  收藏  举报