In the process of repairing the network, workers can take two kinds of operations at every moment, repairing a computer, or testing if two computers can communicate. Your job is to answer all the testing operations.
1. "O p" (1 <= p <= N), which means repairing computer p.
2. "S p q" (1 <= p, q <= N), which means testing whether computer p and q can communicate.
The input will not exceed 300000 lines.
4 1 0 1 0 2 0 3 0 4 O 1 O 2 O 4 S 1 4 O 3 S 1 4Sample Output
FAIL
SUCCESS
#include<stdio.h> #include<math.h> #include<string.h> #include<algorithm> #include<iostream> #include<queue> #define inf 0x3f3f3f3f #define ll long long using namespace std; #define mm 1000000000 #define mem(a,b) memset(a,b,sizeof(a)) int max(int a,int b){ return a>b?a:b;} int maxn (int a,int b,int c){return max(max(a,b),max(b,c));} int min(int a,int b){return a<b?a:b;} using namespace std; struct node { int x,y; int flag; }; int n,m,i; node p[20005]; int vis[20005]; int judge(int a,int b) { if(((p[a].x-p[b].x)*(p[a].x-p[b].x)+(p[a].y-p[b].y)*(p[a].y-p[b].y))<=m*m) return 1; return 0; } int find (int c) { int x=c; while(vis[x]!=x) { x=vis[x]; } return x; } int init (int c) { int i; for(i=1;i<=n;i++) { if(p[i].flag&&judge(i,c)&&c!=i) { vis[find(c)]=find(i); } } return 0; } int main() { scanf("%d%d",&n,&m); for(i=1;i<=n;i++) { scanf("%d%d",&p[i].x,&p[i].y); // p[i].number=i+1; p[i].flag=0; } for(i=1;i<20005;i++) vis[i]=i; char a[20]; getchar(); while(~scanf("%s",&a)) { getchar(); if(a[0]=='O') { int c; scanf("%d",&c); p[c].flag=1; init(c); } else if(a[0]=='S') { int c,b; scanf("%d%d",&b,&c); if(find (b)==find(c)) printf("SUCCESS\n"); else printf("FAIL\n"); } getchar(); } return 0; }