#P3072. [USACO13FEB] Perimeter S

    ID: 758 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: (无) 上传者: 标签>深度优先搜索DFS线段树USACO2013

[USACO13FEB] Perimeter S

题目背景

征求翻译。如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献。

题目描述

Farmer John has arranged N hay bales (1 <= N <= 50,000) in the middle of one of his fields. If we think of the field as a 1,000,000 x 1,000,000 grid of 1 x 1 square cells, each hay bale occupies exactly one of these cells (no two hay bales occupy the same cell, of course).

FJ notices that his hay bales all form one large connected region, meaning that starting from any bale, one can reach any other bale by taking a series of steps either north, south, east, or west onto directly adjacent bales. The connected region of hay bales may however contain "holes" -- empty regions that are completely surrounded by hay bales.

Please help FJ determine the perimeter of the region formed by his hay bales. Note that holes do not contribute to the perimeter.

输入格式

* Line 1: The number of hay bales, N.

* Lines 2..1+N: Each line contains the (x,y) location of a single hay bale, where x and y are integers both in the range

1..1,000,000. Position (1,1) is the lower-left cell in FJ's field, and position (1000000,1000000) is the upper-right cell.

输出格式

* Line 1: The perimeter of the connected region of hay bales.

题目大意

农夫约翰已经在他的一片田地中间放置了n(1<=n<=50000)个干草堆。我们可以认为这片田地是由1000000*1000000 个小方格组成的矩阵,每个干草堆占据一个小方格(当然,没有两堆干草占据同一个格子)

FJ 注意到他的干草堆组成了一个大的连通块,这就意味着从任何一个草堆走起,可以通过相邻草堆走若干步到达其他任意的草堆。这个连通块的内部可能包含若干个“洞”——被干草堆完全包围的空白格子。

请帮助FJ计算整个连通块的周长。计算周长时请不要考虑“洞”。

输入格式

第一行:干草堆的数量 n

第2~n+1行:每行两个数,表示干草堆的坐标(x,y),满足1<=x,y<=1000000

输出格式

连通块的周长p

8 
10005 200003 
10005 200004 
10008 200004 
10005 200005 
10006 200003 
10007 200003 
10007 200004 
10006 200005 

14 

提示

The connected region consisting of hay bales looks like this:

XX X XX XXX

The length of the perimeter of the connected region is 14 (for example, the left side of the region contributes a length of 3 to this total). Observe that the hole in the middle does not contribute to this number.