北大ACM2236——Wireless Network~~并查集

这一题,题目的大概意思是:有N台电脑,彼此直接能通信的最大距离为T,这些电脑因为地震损坏了,需要修理。修理之后,就可以跟其他的修理过的距离小于等于T的电脑通信,你需要回答的是某两台电脑是否能够通信。

简单的并查集的应用,只是加了一个限制条件。

输入N和T,下面N行为N个电脑的坐标。

再下面的是一直到文件结尾,输入O k,表示k电脑进行修理。输入S i j ,表示问你这两台电脑是否能够通信。

我用一个dis数组来存每两台电脑之间的距离,方便后面的判断。

下面是AC的代码,有详细的注释:

#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;

double dis[1005][1005];               //每台电脑与其他电脑之间的距离数组
int par[1005];                        //并查集
bool vis[1005];                       //标记该电脑是否已经修理过
int x[1005], y[1005];
int N, T;

int finds(int x)                     //并查集查找函数
{
	if(x == par[x])
		return x;
	else
		return par[x] = finds(par[x]);
}
void join(int x, int y)             //并查集合并函数
{
	x = finds(x);
	y = finds(y);
	if(x != y)
		par[y] = x;
}
int main()
{
//	freopen("data.txt", "r", stdin);
	int i, j, k, a , b;
	char str;
	scanf("%d%d", &N, &T);
	for(i = 1; i <= N; i++)                     //输入每台电脑坐标
	{
		scanf("%d%d", &x[i], &y[i]);
		par[i] = i;                             //初始化并查集
		vis[i] = false;                         //标记为没有修理过
	}
	getchar();
	for(i = 1; i <= N; i++)                     //计算每两台电脑直接的距离
	{
		for(j = 1; j <= i; j++)
		{
			if(i == j)
				dis[i][j] = 0.0;
			else
				dis[i][j] = dis[j][i] = sqrt(double((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])));
		}
	}
	while(scanf("%c", &str) != EOF)
	{
		if(str == 'O')                         //输入的是 O,要进行修理
		{
			scanf("%d", &k);
			getchar();
			for(i = 1; i <= N; i++)            //与修理过的电脑之间距离的判断,判断是否能通信
			{
				if(i != k && vis[i] && dis[k][i] <= (double)T)
				{
					join(i, k);                //能就合并
				}
			}
			vis[k] = true;                     //标记为修理过
		}
		else if(str == 'S')                    //判断是否能通信
		{
			scanf("%d%d", &a, &b);
			getchar();
			if(finds(a) == finds(b))           //属于同一个并查集,就可以
				printf("SUCCESS\n");
			else
				printf("FAIL\n");
		}
	}
	return 0;
}
时间: 2024-10-09 00:49:56

北大ACM2236——Wireless Network~~并查集的相关文章

POJ 2236 Wireless Network (并查集)

Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 18066   Accepted: 7618 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computer

poj2236 Wireless Network 并查集简单应用

Description 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

poj2236(Wireless Network)并查集

Description 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

Wireless Network 并查集

Wireless 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 compu

POJ2236 Wireless Network 并查集 好题

Wireless Network Description 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 broke

poj 2236 Wireless Network (并查集)

链接:http://poj.org/problem?id=2236 题意: 有一个计算机网络,n台计算机全部坏了,给你两种操作: 1.O x 修复第x台计算机 2.S x,y 判断两台计算机是否联通 联通的条件: 两台都修复好了,求距离小于d 思路: 数据很小,我们可以没修复一台就直接枚举已经修复的计算机找到距离小于d的,放到一个并查集里,查询的话直接查询是否再同一个并查集里就好了 实现代码: //while(scanf("\n%c", &ope) != EOF) #inclu

[LA] 3027 - Corporative Network [并查集]

A very big corporation is developing its corporative network. In the beginning each of the N enterprises of the corporation, numerated from 1 to N, organized its own computing and telecommunication center. Soon, for amelioration of the services, the

POJ1962:Corporative Network(并查集)

Description A very big corporation is developing its corporative network. In the beginning each of the N enterprises of the corporation, numerated from 1 to N, organized its own computing and telecommunication center. Soon, for amelioration of the se

POJ 2236Wireless Network (并查集)

Description 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