GDUT_排位赛题解报告_第2场_Fence Planning

题目:

A. Fence Planning
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Farmer John’s N cows, conveniently numbered 1…N (2≤N≤105), have a complex social structure revolving around “moo networks” — smaller groups of cows that communicate within their group but not with other groups. Each cow is situated at a distinct (x,y) location on the 2D map of the farm, and we know that M pairs of cows (1≤M<105) moo at each-other. Two cows that moo at each-other belong to the same moo network.

In an effort to update his farm, Farmer John wants to build a rectangular fence, with its edges parallel to the x and y axes. Farmer John wants to make sure that at least one moo network is completely enclosed by the fence (cows on the boundary of the rectangle count as being enclosed). Please help Farmer John determine the smallest possible perimeter of a fence that satisfies this requirement. It is possible for this fence to have zero width or zero height.

Input
The first line of input contains N and M. The next N lines each contain the x and y coordinates of a cow (nonnegative integers of size at most 108). The next M lines each contain two integers a and b describing a moo connection between cows a and b. Every cow has at least one moo connection, and no connection is repeated in the input.

Output
Please print the smallest perimeter of a fence satisfying Farmer John’s requirements.

Example
inputCopy
7 5
0 5
10 5
5 0
5 10
6 7
8 6
8 4
1 2
2 3
3 4
5 6
7 6
outputCopy
10

这个题就是说,给出各个点坐标,和M条点之间路径,求一个能罩住某一个连通块的长方形最小周长。

题意很简单,但是我并查集没有学到位,竟然不是到father函数的路径压缩return fa[a]=father(fa[a]);疯狂tle,打完比赛,一看,被这种路径压缩神仙操作秀到了。

思路就是通过并查集和简单枚举找到每一个连通块的答案,然后输出最小那个就好了。
代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <queue>
#include <stack>
#include <map>
//鬼畜头文件
using namespace std;
const int INF = 0x3f3f3f3f;
//1.06e9大小
const int mod = 1e9+7;
typedef unsigned long long ULL;
typedef long long LL;
//鬼畜define
int n,m;
int all[100010][2];
int fa[100010];
typedef struct coor
{
	int MINX,MAXX,MINY,MAXY;
};
int father(int k)
{
	if(fa[k]==k)return k;
	else return fa[k]=father(fa[k]);
}
coor ans[100010];
int main()
{
	scanf("%d %d",&n,&m);
	for(int time=0;time<n;time++)
	{
		fa[time]=time;
		scanf("%d %d",&all[time][0],&all[time][1]);
	}
	for(int time=0;time<m;time++)
	{
		int from,to;
		scanf("%d %d",&from,&to);
		from--;
		to--;
		fa[father(from)]=father(to);
	}
	for(int time=0;time<n;time++)
	{
		father(time);
	}
	int num=0;
	for(int time=0;time<n;time++)
	{
		if(fa[time]==time)
		{
			ans[time].MINX=INF;
			ans[time].MAXX=0;
			ans[time].MINY=INF;
			ans[time].MAXY=0;
		}
	}
	int C=INF;
	for(int time=0;time<n;time++)
	{
		//MINX,MAXX,MINY,MAXY
		ans[father(time)].MINX=min(ans[father(time)].MINX,all[time][0]);
		ans[father(time)].MAXX=max(ans[father(time)].MAXX,all[time][0]);
		ans[father(time)].MINY=min(ans[father(time)].MINY,all[time][1]);
		ans[father(time)].MAXY=max(ans[father(time)].MAXY,all[time][1]);
	}
	for(int time=0;time<n;time++)
	{
		if(fa[time]==time)
		{
			//printf("%d %d %d %d\n",ans[father(time)].MAXY,ans[father(time)].MINY,ans[father(time)].MAXX,ans[father(time)].MINX);
			C=min(C,ans[father(time)].MAXY-ans[father(time)].MINY+ans[father(time)].MAXX-ans[father(time)].MINX);
		}
	}
	printf("%d\n",C*2);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值