标签:getc network getchar bin scanf The cat cst body
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 50348 | Accepted: 20619 |
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
Source
[Submit] [Go Back] [Status] [Discuss]
有n个坏掉的卫星,每次操作‘O’可以修好一个,如果卫星之间都是‘修好的’状态且间距小于‘d‘,就可以通讯。
并查集的时候判断一下两卫星之间距离即可。
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <cmath> 5 #include <iostream> 6 7 #define dbg(x) cout << #x << "=" << x << endl 8 9 using namespace std; 10 const int maxn = 1007; 11 12 int fa[maxn], dis[maxn]; 13 int n,m,ans,cnt; 14 int d; 15 int canuse[maxn]; 16 17 struct node { 18 int x, y; 19 }a[maxn]; 20 21 void init() 22 { 23 for(int i = 1; i <= n; i++) { 24 fa[i] = i; 25 } 26 } 27 28 double cal(node a, node b) { 29 return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y) * (a.y - b.y)); 30 } 31 32 int fid(int x) 33 { 34 int r = x; 35 while(fa[r] != r) { 36 //if(cal(a[fa[r]],a[r]) <= d) 37 r = fa[r]; 38 } 39 int i,j;///路径压缩 40 i = x; 41 while(fa[i] != r) { 42 //if(cal(a[fa[i]],a[r]) <= d) { 43 j = fa[i]; 44 fa[i] = r; 45 i = j; 46 47 } 48 return r; 49 } 50 51 void join(int r1, int r2)///合并 52 { 53 int fidroot1 = fid(r1), fidroot2 = fid(r2); 54 if(fidroot1 != fidroot2) { 55 fa[fidroot2] = fidroot1; 56 } 57 } 58 59 int main() 60 { 61 scanf("%d %d",&n, &d); 62 init(); 63 for(int i = 1; i <= n; ++i) { 64 scanf("%d %d",&a[i].x, &a[i].y); 65 } 66 getchar(); 67 char ch[2]; 68 int p,q; 69 int cnt = 1; 70 while(scanf("%s", ch) != EOF) { 71 //dbg(ch[0]); 72 if(ch[0] == ‘O‘) { 73 scanf("%d",&p); 74 //getchar(); 75 canuse[cnt++] = p; 76 for(int i = 1; i < cnt-1; ++i) { 77 if(cal(a[canuse[i]],a[p]) <= double(d)) { 78 join(canuse[i],p); 79 } 80 } 81 } 82 if(ch[0] == ‘S‘) { 83 scanf("%d %d",&p,&q); 84 //getchar(); 85 86 //join(p,q); 87 if(fid(p) == fid(q)) { 88 puts("SUCCESS"); 89 } 90 else { 91 puts("FAIL"); 92 } 93 } 94 } 95 return 0; 96 }
A - Wireless Network POJ - 2236-kuangbin带你飞
标签:getc network getchar bin scanf The cat cst body
原文地址:https://www.cnblogs.com/orangeko/p/12264155.html