输入
输出
示例输入
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
思路:
对于O操作,每修理一个电脑就检测该电脑与所有被检测过的电脑之间能否通信,可以就把两个电脑加入同一个集合中
对于S操作,判断两台电脑是否在都修理过且在同一个集合中,满足就输出SUCCESS,否则输出FAIL
代码:
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <string>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
//#include <unordered_map>
#define Fbo friend bool operator < (node a, node b)
#define mem(a, b) memset(a, b, sizeof(a))
#define FOR(a, b, c) for (int a = b; a <= c; a++)
#define RFOR(a, b, c) for (int a = b; a >= c; a--)
#define off ios::sync_with_stdio(0)
#define sc(a) scanf("%d",&a)
bool check1(int a) { return (a & (a - 1)) == 0 ? true : false; }
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int Maxn = 1e5 + 5;
const double pi = acos(-1.0);
const double eps = 1e-8;
int a[Maxn],n,d;
int b[Maxn], cnt=0;//被修理的电脑编号
bool vis[Maxn];
struct node {
int x, y;
}point[Maxn];
void init() {
for (int i = 1; i < Maxn; i++)
a[i] = i;
}
int find(int x) {
return x == a[x] ? x : find(a[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x != y) {
a[x] = a[y];
}
}
bool check(int a, int b) {
double dis = sqrt((double)((point[a].x - point[b].x) * (point[a].x - point[b].x)) + ((point[a].y - point[b].y) * (point[a].y - point[b].y)));
//cout << dis << "---" << d << endl;
return dis <= d;
}
int main() {
off;
mem(vis, 0);
cin >> n >> d;
init();
for (int i = 1; i <= n; i++) {
cin >> point[i].x >> point[i].y;
}
char ch;
while (cin >> ch) {
if (ch == ‘O‘) {
int m;
cin >> m;
vis[m] = 1;
for (int i = 0; i < cnt; i++) {
if (check(b[i], m)) {
unite(b[i], m);
}
}
b[cnt++] = m;
}
else {
int l, r;
cin >> l >> r;
if (vis[l] && vis[r] && find(l) == find(r)) {
cout << "SUCCESS" << endl;
}
else cout << "FAIL" << endl;
}
}
return 0;
}
(并查集模板题)Wireless Network POJ - 2236
原文:https://www.cnblogs.com/AlexLINS/p/12656000.html