#P5429. [USACO19OPEN] Fence Planning S

[USACO19OPEN] Fence Planning S

Description

Farmer John has NN cows, numbered 1N1 \ldots N (2N1052 \leq N \leq 10^5). They have a complex social network based on “moo-networks”: groups of cows that communicate with each other within the group but do not communicate with cows in other groups.

Each cow is at a distinct position (x,y)(x, y) on a 2D map of the farm, and we know that there are MM pairs of cows (1M<1051 \leq M < 10^5) that moo to each other. Two cows that moo to each other belong to the same moo-network.

To upgrade his farm, Farmer John wants to build a rectangular fence whose four sides are parallel to the xx-axis and the yy-axis. He wants at least one moo-network to be completely enclosed by the fence (cows on the boundary of the rectangle are considered enclosed). Please help Farmer John find the minimum possible perimeter of a fence that satisfies his requirement. It is possible that the fence has width 00 or height 00.

Input Format

The first line contains NN and MM. The next NN lines each contain the xx coordinate and yy coordinate of a cow (non-negative integers up to 10810^8). The next MM lines each contain two integers aa and bb, indicating that cows aa and bb have a mooing relationship. Each cow has at least one mooing relationship, and there are no duplicate mooing relationships in the input.

Output Format

Output the minimum perimeter of a fence that satisfies Farmer John’s requirement.

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
10

Hint

Translated by ChatGPT 5