Description
An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broken. The computers are repaired one by one, and the network gradually began to work again. Because of the hardware restricts, each computer can only directly communicate with the computers that are not farther than d meters from it. But every computer can be regarded as the intermediary of the communication between two other computers, that is to say computer A and computer B can communicate if computer A and computer B can communicate directly or there is a computer C that can communicate with both A and B.
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.
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.
Input
The first line contains two integers N and d (1 <= N <= 1001, 0 <= d <= 20000). Here N is the number of computers, which are numbered from 1 to N, and D is the maximum distance two computers can communicate directly. In the next N lines, each contains two integers xi, yi (0 <= xi, yi <= 10000), which is the coordinate of N computers. From the (N+1)-th line to the end of input, there are operations, which are carried out one by one. Each line contains an operation in one of following two formats:
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.
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.
Output
For each Testing operation, print "SUCCESS" if the two computers can communicate, or "FAIL" if not.
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
题意:有n台坏掉的电脑 现在告诉你有两个操作 一个是修复一台电脑 一个是询问两台电脑是否联通
思路:看到n才1000 果断暴力先把距离符合要求的点建图 然后就并查集(输出用puts可以降到1s)
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #include<vector> #include<stack> #include<bitset> #include<cstdlib> #include<cmath> #include<set> #include<list> #include<deque> #include<map> #include<queue> #define ll long long int using namespace std; inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;} int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1}; int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1}; const int inf=0x3f3f3f3f; const ll mod=1e9+7; int n,d; struct point{ int x,y; }; point p[1007]; vector<int> v[1007]; int f[1007]; int find(int x){ if(x!=f[x]) f[x]=find(f[x]); return f[x]; } int main(){ //ios::sync_with_stdio(false); while(~scanf("%d%d",&n,&d)){ for(int i=1;i<=n;i++){ scanf("%d%d",&p[i].x,&p[i].y); v[i].clear(); f[i]=-1; } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++){ if(i==j) continue; if((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y)<=d*d){ v[i].push_back(j); } } char t; while(~scanf("%c",&t)){ getchar(); if(t=='S'){ int po1,po2; scanf("%d%d",&po1,&po2); if(find(po1)==find(po2)) printf("SUCCESS "); else printf("FAIL "); }else{ int po; scanf("%d",&po); f[po]=po; for(int i=0;i<v[po].size();i++) if(f[v[po][i]]!=-1) f[find(v[po][i])]=find(po); } getchar(); } } return 0; }