Visit every city in a connected graph using paired outbound and return flights so the ZIP codes in order of first visit form the smallest possible number.
Hard8DFSGraphGreedyNo attempts yetTime limit5sMemory limit512 MBYour boss is sending you on an international sales trip.
You have N cities, numbered 1 to N, to visit, and there is a set of bidirectional flights between the cities.
Every city must be visited at least once. To do that you can book any number of tickets, subject to the following conditions.
You could try to minimize the total distance travelled, but you did that last time, so that would be boring. Instead you use the fact that each city has a distinct 5 digit ZIP code. When you visit a city for the first time, and this includes the city you start from, you write down its ZIP code, and you concatenate the codes in the order of the first visits into one large number. Print the smallest number you can achieve.
The first line of the input gives the number of test cases T. T test cases follow.
Each test case starts with a single line containing two integers, the number of cities N and the number of possible bidirectional flights M.
N lines follow, and the i-th of them contains the 5 digit ZIP code of the i-th city. No ZIP code has a leading zero, and all ZIP codes inside one test case are distinct.
M lines follow, each containing two integers i and j (1≤i<j≤N), meaning that a bidirectional flight exists between the i-th city and the j-th city. No flight is given twice inside one test case.
Visiting every city under the rules above is guaranteed to be possible.
Limits
For each test case, output one line containing "Case #x: y", where x is the test case number starting from 1, and y is the smallest number you can achieve by concatenating the ZIP codes along your trip.
Take a test case with 6 cities whose ZIP codes are 10001, 10002, 10003, 10004, 10005, 10006 in city order, and whose flights connect (1, 2), (1, 6), (2, 3), (2, 4), (3, 5), (4, 5). The following trip reaches the smallest number, 100011000210003100041000510006.