#P16492. [GKS 2014 #D] Sort a scrambled itinerary

    ID: 16477 Type: RemoteJudge 1000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 3 Uploaded By: Tags>图论2014哈希 hashingGoogle Kick Start

[GKS 2014 #D] Sort a scrambled itinerary

题目描述

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

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 TT, after which TT cases follow. For each case, it starts with an integer NN. There are NN 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

提示

Limits

1T1001 \le T \le 100.

For each case, the input tickets are messed up from an entire itinerary bought by Mary. In other words, it's ensured can be recovered to a valid itinerary.

Small dataset (Test Set 1 - Visible)

1N1001 \le N \le 100.

Large dataset (Test Set 2 - Hidden)

1N1041 \le N \le 10^4.

(The segment for second case in sample can be seen as below) MIA-ORD, DFW-JFK, SFO-DFW, JFK-MIA