[BOJ 12187] Sort a scrambled itinerary (Large)

View as PDF

Submit solution

Points: 2
Time limit: 5.0s
Memory limit: 512M

Problem types
Allowed languages
Assembly, Awk, C, C++, Java, Pascal, Perl, Python, Sed, Text

Once upon a day, Mary bought a one-way ticket from somewhere to somewhere with some flight transfers.</p>

For example: SFO->DFW DFW->JFK JFK->MIA MIA->ORD.

Obviously, transfer flights at a city twice or more doesn't make any sense. So Mary will not do that.

Unfortunately, after she received the tickets, she messed up the tickets and she forgot the order of the ticket.

Help Mary rearrange the tickets to make the tickets in correct order.

입력 형식

The first line contains the number of test cases T, after which T cases follow.</p>

For each case, it starts with an integer N. There are N flight tickets follow.

Each of the next 2 lines contains the source and destination of a flight ticket.

출력 형식

For each test case, output one line containing "Case #x: itinerary", where x is the test case number (starting from 1) and itinerary is sorted list of flight tickets which represents the actual itinerary. Each flight segment in the itinerary should be outputted as pair of source-destination airport codes.

예제 입력

2
1
SFO
DFW
4
MIA
ORD
DFW
JFK
SFO
DFW
JFK
MIA

예제 출력

Case #1: SFO-DFW
Case #2: SFO-DFW DFW-JFK JFK-MIA MIA-ORD

Comments

There are no comments at the moment.