Choose a start city and a depth-first ticket tour of the graph so the concatenated first-visit ZIP codes form the smallest number.
Medium6BacktrackingDFSGraphNo attempts yetTime limit5sMemory limit512 MBYour company is sending you on an international sales trip. You must visit N cities, numbered 1 to N, and bidirectional flight routes connect some pairs of cities. Every city must be visited at least once.
You travel by booking tickets. A ticket follows these rules.
Each city has a 5 digit ZIP code, and all ZIP codes within one test case are different. Every time you enter a city for the first time, including the city you start from, you write down its ZIP code. Concatenating the ZIP codes in the order you wrote them gives one large number. Find the smallest number you can obtain.
The first line contains the number of test cases T. T test cases follow.
The first line of each test case contains the number of cities N and the number of bidirectional flight routes M.
The next N lines contain the 5 digit ZIP codes of city 1 through city N in order, one per line. No ZIP code starts with 0, and all ZIP codes within one test case are different.
The next M lines each contain two integers i and j (1≤i<j≤N), meaning a bidirectional flight route exists between city i and city j. All routes within one test case are different.
Visiting every city under the rules above is always possible.
For each test case, print one line in the form "Case #x: y", where x is the test case number starting from 1 and y is the smallest number you can obtain.
Consider six cities whose ZIP codes are 10001, 10002, 10003, 10004, 10005 and 10006 in order, with routes (1, 2), (1, 6), (2, 3), (2, 4), (3, 5) and (4, 5). This trip gives the smallest number.
The result is 100011000210003100041000510006, and no smaller number is possible.