#P5429. [USACO19OPEN] Fence Planning S
[USACO19OPEN] Fence Planning S
Description
Farmer John has cows, numbered (). 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 on a 2D map of the farm, and we know that there are pairs of cows () 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 -axis and the -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 or height .
Input Format
The first line contains and . The next lines each contain the coordinate and coordinate of a cow (non-negative integers up to ). The next lines each contain two integers and , indicating that cows and 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
京公网安备 11011102002149号