链接:http://poj.org/problem?id=2236
| Time Limit: 10000MS | Memory Limit: 65536K | |
| Total Submissions: 19684 | Accepted: 8252 |
4 1 0 1 0 2 0 3 0 4 O 1 O 2 O 4 S 1 4 O 3 S 1 4
FAIL SUCCESS
Source
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <map>
//#pragma comment(linker, "/STACK:102400000, 102400000")
using namespace std;
typedef unsigned int li;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const double pi = acos(-1.0);
const double e = exp(1.0);
const double eps = 1e-8;
const int maxn = 1005;
int father[maxn]; // 存储父节点
char str[5]; // 键入命令
int n, dist; // 电脑台数以及连接距离限制
struct point
{ // 存储电脑坐标及其状态(0表示未修好,1表示修好)
int x, y;
bool flag;
} com[maxn];
inline int square(int x); // 计算平方值
inline int distan(point a, point b); // 计算电脑距离的平方
int find(int x); // 查找父节点并路径压缩
void com_union(int x, int y); // 合并能连接到的电脑
int main()
{
ios::sync_with_stdio(false);
int x, y;
while (~scanf("%d%d", &n, &dist))
{
memset(father, -1, sizeof(father)); // 初始化
dist *= dist; // 距离限制的平方
for (int i=1; i<=n; i++)
{
scanf("%d%d", &com[i].x, &com[i].y);
com[i].flag = 0; // 初始化
}
while (~scanf("%s%d", str, &x))
{
if (str[0] == 'O')
{
com[x].flag = 1;
for (int i=1; i<=n; i++) // 找出能连接的电脑
if (i!=x && com[i].flag &&
distan(com[x], com[i])<=dist)
com_union(x, i);
}
else
{
scanf("%d", &y);
x = find(x);
y = find(y); // 找出父节点
printf(x==y ? "SUCCESS\n" : "FAIL\n"); // 父节点相等,则表明连接好了
}
}
}
return 0;
}
inline int square(int x)
{
return (x*x);
}
inline int distan(point a, point b)
{
return square(a.x-b.x)+square(a.y-b.y);
}
int find(int x)
{
if (father[x] < 0)
return x;
return father[x] = find(father[x]); // 此处即为路径压缩
}
void com_union(int x, int y)
{ // 启发式合并,按规模进行
x = find(x);
y = find(y);
if (x == y)
return ;
if (father[x] > father[y])
{ // 根节点的父节点为负数,绝对值为规模大小
father[y] += father[x];
father[x] = y;
}
else
{
father[x] += father[y];
father[y] = x;
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/silenceneo/article/details/47776059