
2005
ASIA PROGRAMMING CONTEST – MANILA
October
29, 2005
Problem G
Gotcha!
For the past few months there has been a series of bank robberies and the police were not able to catch the robbers. Each time the robbers strike, it is executed with précised timing. In all cases, the bank alarm systems were disabled, leaving the bank employees helpless in contacting the police. The police are clueless even with the surveillance videos, as the suspects wear ski masks and gloves, and flee in stolen vehicles.
A break came about when a badly tortured man was found left for dead in an alley. This man turned out to be the IT administrator of the first bank that was robbed. The police were able to gather from him that a group of men abducted him and tortured him to give out the password that allows access to the bank’s computer. The police quickly found out that all banks that were robbed had one thing in common: the banks’ computer systems had been accessed!
You were asked by the police to help solve the spree of bank robberies. You checked the computer systems of all the robbed banks. You found out that the suspects accessed the feature that lists down all the banks having computers directly connected to the current bank’s computer including its physical distance. The obtained list becomes the list to look for the next bank to rob.
Studying carefully the pattern of bank robberies, you discovered that the first bank was chosen due to the fact that the tortured man is an employee of the first bank. The second bank is the bank closest to the first bank; this is based on the list obtained in the first bank. With lists obtained from the first and second banks, the third bank is the next closest bank based on the two lists. In the event that the list contains more than one bank of the same distance, the one from the older list is chosen. The more lists the suspects obtain the more bank robberies are reported; however, no bank will be robbed more than once.
The input consists of several test cases. The first line of the input file indicates the number of test cases. Each case starts with the number of banks, the first victim and the number of banks robbed so far. This is followed by a series of strings consisting of the bank code, number of connections, and connected banks consisting of bank codes and physical distance.
The output is the bank code of the next bank to be robbed (as you want the police to be prepared for the next bank robbery).
Input |
Sample Output |
|
1 10,5,4 1,3,2,6,4,7,8,8 2,2,9,9,10,10 3,2,4,11,6,12 4,2,1,13,3,14 5,3,7,1,9,2,10,3 6,2,3,15,9,16 7,2,1,4,10,5 8,2,1,17,9,18 9,3,5,19,6,20,8,21 10,3,2,22,7,23,5,24 |
Case 1: 1 |