题意:
有n台损坏的电脑,现要将其逐台修复,且使其相互恢复通信功能。若两台电脑能相互通信,则有两种情况,一是他们之间的距离小于d,二是他们可以借助都可到达的第三台已修复的电脑。给出所有电脑的坐标位置,对其进行两种可能的操作,O x表示修复第x台,S x y表示判断x y之间能否通信,若能输出SUCCESS,否则输出FALL。
Input:
第一行包含两个整数n和d(1<=n<=1001,0<=d<=20000)。这里n是从1到n的计算机数量,d是两台计算机可以直接通信的最大距离。在n行中,每一个包含两个整数Xi,Yi(0<=Xi,Yi=10000),这是n个计算机的坐标。从(n+1)-第行到输入端,都有一个一个地执行的操作。每行包含以下两种格式之一的操作: 1.“o p”(1<=p<=n),这意味着修复计算机p。 2.“s p q“(1<=p,q<=n),这意味着测试计算机p和q是否可以通信。 输入不超过300000行。
Output:
对于每个测试操作,如果两台计算机可以通信,则打印“成功”,否则打印“失败”。
思路:
并查集:一个等价类的就可以互相通信;每次修好一个电脑,就把它能够通信(已修好且距离小于d)的电脑都与它自己联通
#include <iostream>
#include <string>
#include <cmath>
#include <ctype.h>
#include <memory.h>
#include <string.h>
#include <algorithm>
#include <map>
#include <iomanip>
#include <set>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#define ll long long int
using namespace std;
const int maxn = 1010 ;
struct jgt
{
int x, y;
} ;
int n, d;
jgt a[ maxn] ;
int par[ maxn] ;
bool is_work[ maxn] ;
int MAP[ maxn] [ maxn] ;
int pow2 ( int x)
{
return x * x;
}
int find ( int x)
{
if ( par[ x] == x)
return x;
return par[ x] = find ( par[ x] ) ;
}
int main ( )
{
cin >> n >> d;
for ( int i = 1 ; i <= n; i++ )
{
cin >> a[ i] . x >> a[ i] . y;
par[ i] = i;
}
for ( int i = 1 ; i <= n; i++ )
for ( int j = 1 ; j <= n; j++ )
{
if ( pow2 ( a[ i] . x - a[ j] . x) + pow2 ( a[ i] . y - a[ j] . y) <= pow2 ( d) )
{
MAP[ i] [ j] = j;
MAP[ j] [ i] = i;
}
}
char c;
while ( cin >> c)
{
if ( c == 'O' )
{
int num;
cin >> num;
is_work[ num] = true ;
for ( int i = 1 ; i <= n; i++ )
{
if ( is_work[ MAP[ num] [ i] ] )
{
int f = find ( MAP[ num] [ i] ) ;
par[ f] = num;
}
}
}
else
{
int a, b;
cin >> a >> b;
int fa = find ( a) ;
int fb = find ( b) ;
if ( fa == fb)
cout << "SUCCESS" << endl;
else
cout << "FAIL" << endl;
}
}
return 0 ;
}
#include <iostream>
#include <string>
#include <cmath>
#include <ctype.h>
#include <memory.h>
#include <string.h>
#include <algorithm>
#include <map>
#include <iomanip>
#include <set>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#define ll long long int
using namespace std;
const int maxn = 1010 ;
struct jgt
{
int x, y;
} ;
int n, d;
jgt a[ maxn] ;
int par[ maxn] ;
bool is_work[ maxn] ;
vector< int > v[ maxn] ;
int pow2 ( int x)
{
return x * x;
}
int find ( int x)
{
if ( par[ x] == x)
return x;
return par[ x] = find ( par[ x] ) ;
}
int main ( )
{
cin >> n >> d;
for ( int i = 1 ; i <= n; i++ )
{
cin >> a[ i] . x >> a[ i] . y;
par[ i] = i;
}
for ( int i = 1 ; i <= n; i++ )
for ( int j = 1 ; j <= n; j++ )
{
if ( pow2 ( a[ i] . x - a[ j] . x) + pow2 ( a[ i] . y - a[ j] . y) <= pow2 ( d) )
{
v[ i] . push_back ( j) ;
v[ j] . push_back ( i) ;
}
}
char c;
while ( cin >> c)
{
if ( c == 'O' )
{
int num;
cin >> num;
is_work[ num] = true ;
for ( int i = 0 ; i < v[ num] . size ( ) ; i++ )
if ( is_work[ v[ num] [ i] ] )
{
int f = find ( v[ num] [ i] ) ;
par[ f] = num;
}
}
else
{
int a, b;
cin >> a >> b;
int fa = find ( a) ;
int fb = find ( b) ;
if ( fa == fb)
cout << "SUCCESS" << endl;
else
cout << "FAIL" << endl;
}
}
return 0 ;
}