문제
Bessie is located in a network consisting of N
(
and
Each vertex
Your current location can be represented by an ordered pair
where
Change the current vertex by moving through the current portal.
Switch the current portal. At each vertex, the first two portals in the list are paired up, while the last two portals in the list are also paired up. That is, if your current location is
(v,pv,2) you may switch to use the portal (v,pv,1) , and vice versa. Similarly, if your current location is (v,pv,3) you may switch to use the portal (v,pv,4 ) and vice versa. No other switches are allowed (e.g., you may not switch from portalpv,2 to portalpv,4 ).
There are
For example, if you permute the portals adjacent to
If you are currently at portal
If you are currently at portal
You may no longer switch from portal
Compute the minimum total amount of moonies required to modify the network in order to make it possible to reach every possible location from every other location. It is guaranteed that the test data is constructed in such a way that there exists at least one valid way of modifying the network.
입력
The first line contains
The next
It is guaranteed that for each
출력
A single line containing the minimum total amount of moonies required to modify the network in order to make it possible to reach every possible location from every other location.
예제1
5
10 1 4 8 9
11 1 2 5 6
12 9 10 2 3
3 4 3 6 7
15 10 8 7 5
13
It suffices to permute the adjacency lists of vertices 1
and