POJ——2236 Wireless Network (and check the template questions)

Link to the original question: http://poj.org/problem?id=2236

Insert picture description here
Test sample

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

Meaning: there is nnN computers on the plane are broken. It is known that the direct communication distance between two computers isddd (that is, if the direct distance between two repaired computers is less than or equal toddd , you can communicate directly. ). Now perform a series of operations: ifO x O \space xO x  , then this operation is numberedxxx computer to repair. IfS x y S\space x\space yS x y   , the test number isxxx and numberyyy 's computer can communicate, you need to output the answer.

Problem-solving ideas: a very interesting union search, why? Because the merge operation is for us to do it ourselves. Every time the system performs OOO operation, we have to judge whether all computers can communicate. Whether it can communicate is whether it is in the same set, here is the template and check set. The important point is how do we merge?We want to introduce a vis visThe v i s array is used to determine whether the computer has been repaired. If it is not repaired, it cannot be merged. We also need to determine whether the distance between the two computers is less than or equal toddd . These two restrictions determine whether communication is possible.Knowing this, this question is easy to solve. Look at the code specifically.

AC code

/*
*邮箱:[email protected]
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<iostream>	//POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e3+4;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll>  pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int n,d;//每台电脑所能通信的最大距离。
bool vis[maxn];//判断电脑是否修好。
int point[maxn][2];//存储每台电脑的坐标
int father[maxn];//存储电脑所在集合。
int Find(int x){
    
    
	int r=x;
	while(r!=father[r]){
    
    
		r=father[r];
	}
	int i=x,j;
	while(father[i]!=r){
    
    
		j=father[i];
		father[i]=r;
		i=j;
	}
	return r;
}
int main(){
    
    
	//freopen("in.txt", "r", stdin);//提交的时候要注释掉
	IOS;
	while(cin>>n>>d){
    
    
		rep(i,1,n){
    
    
			cin>>point[i][0]>>point[i][1];
			father[i]=i;
			vis[i]=false;
		}
		string op;
		int temp1,temp2;
		while(cin>>op){
    
    
			if(op=="O"){
    
    
				cin>>temp1;
				vis[temp1]=true;
				rep(i,1,n){
    
    
					if(i!=temp1&&vis[i]){
    
    
						int dx=point[temp1][0]-point[i][0];
						int dy=point[temp1][1]-point[i][1];
						if(dx*dx+dy*dy<=d*d){
    
    
							int fx=Find(i);
							int fy=Find(temp1);
							if(fx!=fy){
    
    
								father[fx]=fy;
							}
						}
					}
				}
			}
			else{
    
    
				cin>>temp1>>temp2;
				if(Find(temp1)!=Find(temp2)){
    
    
					cout<<"FAIL"<<endl;
				}
				else{
    
    
					cout<<"SUCCESS"<<endl;
				}
			}
		}
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/hzf0701/article/details/108994939