#t538. 连接格点
连接格点
Connecting a Grid of Points
Special for beginners, ^_^
Problem Description
There is a grid of points with rows and columns; adjacent points may be connected. A vertical connection costs one unit, and a horizontal connection costs two units. Some pairs of points are already connected. Find the minimum number of additional units needed to make all points connected.
Input Format
The first line contains two positive integers and .
The following lines each contain four positive integers , meaning the point at row , column and the point at row , column are already connected. The input guarantees .
Output Format
The minimum additional cost needed to connect all points.
Sample Input 1
2 2
1 1 2 1
Sample Output 1
3
Constraints
1s, 1024KiB for each test case.
of the data: .
of the data: .