Woburn Challenge 2016-17 Round 4 - Senior Division

"Hopps, Wilde… parking duty. Dismissed."
"Just kidding! We have reports of a street racer tearing up Savannah
Central. Find him. Shut him down."
Nick has ended up joining the ZPD as Zootopia's first fox cop, and as Judy's partner, no less! They've been assigned a case to track down a dangerous street racing criminal, but it won't be easy - they'll need to do some investigative work before they can begin to assemble a list of suspects.
Zootopia has
key locations, with
roads
running amongst them. The
road runs between distinct locations
and
, and can be traveled
along in either direction in
minute. It's possible to reach every
location from every other location by following a sequence of roads.
The ZPD headquarters are located in location , and aside from that,
some other locations may be of significance to the investigation. For
each location
, if
, then Judy and Nick believe that it
contains a clue, and will need to go check it out. Otherwise, if
, then the location is of no interest to them. At least one location
besides location
is guaranteed to contain a clue.
Judy and Nick will both start at the ZPD headquarters, and will then split up to each travel around Zootopia, inspecting all of the locations that contain clues. On the way, each of them may travel through any locations as many times as they'd like, and they may both be present in the same location at the same time. They've agreed to meet back at the headquarters once they're done. What's the minimum amount of time in which Judy and Nick can independently travel around by following sequences of roads, such that they both end up back at the ZPD headquarters, and such that every location with a clue ends up being visited by at least one of the two cops?
In test cases worth of the points,
and
for each
.
In test cases worth another of the points,
and
for each
.
In test cases worth another of the points,
for each
.
Input Specification
The first line of input consists of a single integer .
lines follow, the
of which consists of a single integer
(for
).
lines follow, the
of which consists of two
space-separated integers
and
(for
).
Output Specification
Output one line consisting of a single integer - the minimum number of minutes required for Judy and Nick to visit all locations with clues (between the two of them) and both return to the ZPD headquarters.
Sample Input
7
1
1
0
1
1
1
1
1 2
2 3
1 4
5 4
6 4
7 4
Sample Output
6
Sample Explanation
Judy can complete the following route in minutes:
During the same minutes, Nick can complete the following route:
All locations with clues end up being visited by either Judy or Nick
at least once.
Comments