#P5429. Moo Network Enclosure
Moo Network Enclosure
Moo Network Enclosure
Farmer John has \( N \) cows (numbered \( 1 \ldots N \), where \( 2 \le N \le 10^5 \)) placed at distinct positions on a 2D map, and there are \( M \) moo calls (with \( 1 \le M < 10^5 \)) between pairs of cows. Two cows that moo to each other belong to the same moo network.
Farmer John wants to build a rectangular fence whose sides are parallel to the \( x \)- and \( y \)-axes such that at least one moo network is completely enclosed by the fence. A cow on the boundary of the rectangle is considered enclosed. Note that the fence may have a width or height of 0 (thus, the perimeter could be 0).
Your task is to help Farmer John determine the minimum possible perimeter of such a fence. In other words, for each moo network (i.e. connected component of cows according to the moo calls), compute the perimeter of the smallest axis-aligned rectangle that encloses all the cows in that network, and output the minimum perimeter among all networks.
The perimeter of an axis-aligned rectangle that spans from \( x_{min} \) to \( x_{max} \) and from \( y_{min} \) to \( y_{max} \) is given by:
[ P = 2 \times \big((x_{max} - x_{min}) + (y_{max} - y_{min})\big) ]
inputFormat
The first line contains two integers \( N \) and \( M \).
The next \( N \) lines each contain two integers \( x \) and \( y \), representing the coordinates of a cow. It is guaranteed that no two cows share the same position.
The next \( M \) lines each contain two integers \( a \) and \( b \) (1-indexed), indicating that cow \( a \) and cow \( b \) moo to each other (and hence belong to the same network).
outputFormat
Output a single integer representing the minimum possible perimeter of a rectangular fence that completely encloses at least one moo network.
sample
4 2
1 1
3 1
1 4
3 4
1 2
3 4
4