题目:
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
下面是我的AC代码:
#include"stdio.h"
#include "math.h"
const int MAX_SIZE = 1002;
class UFS
{
public:
int ancestor[MAX_SIZE];
int degree[MAX_SIZE];
bool repaired[MAX_SIZE];
int xi[MAX_SIZE];
int yi[MAX_SIZE];
void MakeSet();
int FindSet(const int& x);
void Union(const int& x,const int& y);
};
void UFS::MakeSet()
{
for(int i = 0;i<MAX_SIZE;++i)
{
this->ancestor[i] = i;
this->degree[i] = 0;
this->repaired[i] = false;
this->xi[i] = 0;
this->yi[i] = 0;
}
}
int UFS::FindSet(const int& x)
{
if(ancestor[x] != x)
{
ancestor[x] = FindSet(ancestor[x]);
}
return ancestor[x];
}
void UFS::Union(const int& x,const int& y)
{
int xAncestor = this->FindSet(x);
int yAncestor = this->FindSet(y);
if(xAncestor == yAncestor)
{
return ;
}
if(degree[xAncestor] < degree[yAncestor])
{
ancestor[xAncestor] = yAncestor;
}
else
{
if(degree[xAncestor] == degree[yAncestor])
{
degree[xAncestor] += 1;
}
ancestor[yAncestor] = xAncestor;
}
}
int N,d;
int main(void)
{
UFS ufs;
ufs.MakeSet();
scanf("%d %d",&N,&d);
for(int i = 1;i<=N;++i)
{
scanf("%d %d",&ufs.xi[i],&ufs.yi[i]);
}
char c;
int c1,c2;
while(scanf("%c",&c)!=EOF)
{
if(c == 'O')
{
scanf(" %d",&c1);
ufs.repaired[c1] = true;
for(int j = 1;j<=N;j++)
{
if(ufs.repaired[j] == true)
{
if(pow(ufs.xi[c1]-ufs.xi[j],2) + pow(ufs.yi[c1]-ufs.yi[j],2)<= d*d)
{
ufs.Union(c1,j);
}
}
}
}
if(c == 'S')
{
scanf(" %d %d",&c1,&c2);
if(ufs.FindSet(c1) == ufs.FindSet(c2))
{
printf("SUCCESS/n");
}
else
{
printf("FAIL/n");
}
}
}
return 0;
}