POJ 2236Wireless Network [并查集]
Posted 布图
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 2236Wireless Network [并查集]相关的知识,希望对你有一定的参考价值。
题目
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.
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.
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
代码
开始还以为哪里错了,超时了
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <vector>
#include <limits.h>
#include <assert.h>
#include <functional>
#include <numeric>
#include <ctime>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define ppb pop_back
#define lbnd lower_bound
#define ubnd upper_bound
#define endl '\\n'
#define trav(a, x) for(auto& a : x)
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define sz(x) (ll)x.size()
#define hell 1000000007
#define DEBUG cerr<<"/n>>>I'm Here<<</n"<<endl;
#define display(x) trav(a,x) cout<<a<<" ";cout<<endl;
#define what_is(x) cerr << #x << " is " << x << endl;
#define ini(a) memset(a,0,sizeof(a))
#define case ll T;read(T);for(ll Q=1;Q<=T;Q++)
#define lowbit(x) x&(-x)
#define pr printf
#define sc scanf
#define _ 0
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DBG(x) \\
(void)(cout << "L" << __LINE__ \\
<< ": " << #x << " = " << (x) << '\\n')
#define TIE \\
cin.tie(0);cout.tie(0);\\
ios::sync_with_stdio(false);
//#define long long int
//using namespace __gnu_pbds;
template <typename T>
void read(T &x) {
x = 0;
int f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + (ch ^ 48);
ch = getchar();
}
x *= f;
return;
}
inline void write(long long x) {
if(x<0) putchar('-'), x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
putchar('\\n');
}
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 1009;
const ll N = 5;
struct Node {
int x, y;
}arr[maxn];
int fa[maxn];
int mx[maxn];
set<int> se;
int get(int x) {
if (x == fa[x]) return x;
return fa[x] = get(fa[x]);
}
double mul(double x1, double y1, double x2, double y2) {
return (double)sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
}
void solve(){
for (int i=1; i<maxn; i++) fa[i] = i;
char ch;
int n, d, x, y, cnt = 0;
cin>>n>>d;
// read(n), read(d);
for (int i=1; i<=n; i++) {
cin>>arr[i].x>>arr[i].y;
// read(arr[i].x), read(arr[i].y);
}
while (cin>>ch) {
if (ch == 'S') {
cin>>x>>y;
x = get(x), y = get(y);
if (x == y) cout<<"SUCCESS"<<endl;
else cout<<"FAIL"<<endl;
} else if (ch == 'O') {
cin>>x;
if (!se.count(x)) {
se.insert(x);
mx[cnt++] = x;
for (int i=0; i<cnt-1; i++) {
int j = cnt-1;
int x1 = arr[mx[i]].x;
int y1 = arr[mx[i]].y;
int x2 = arr[mx[j]].x;
int y2 = arr[mx[j]].y;
if (mul(x1, y1, x2, y2) <= d) {
int p = get(mx[i]), q = get(mx[j]);
if (p!=q)
fa[p] = q;
}
}
}
}
}
}
int main()
{
// TIE;
// #ifndef ONLINE_JUDGE
// freopen ("in.txt" , "r", stdin );
// freopen ("out.txt", "w", stdout);
// #else
// #endif
solve();
// case{solve();}
// case{cout<<"Case "<<Q<<":"<<endl;solve();}
return ~~(0^_^0);
}
以上是关于POJ 2236Wireless Network [并查集]的主要内容,如果未能解决你的问题,请参考以下文章
POJ 2236Wireless Network [并查集]
POJ - 2236Wireless Network (并查集)
POJ - 2236Wireless Network (并查集)
Qualcomm Atheros QCA9565 / AR9565 Wireless Network Adapter (rev 01)