## Maverick

never stop thinking

# Google中国2015校园招聘笔试Round D APAC Test Problem C. Sort a scrambled itinerary

### Problem

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.

### Input

The first line contains the number of test cases T, after which T cases follow.
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.

### Output

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.

### Limits

1 ≤ T ≤ 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.

1 ≤ N ≤ 100.

#### Large dataset

1 ≤ N ≤ 104.

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

### Sample

 Input Output 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 

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <string>
#include <set>
#include <map>
using namespace std;

int n;
map<string,string> mp;
set<string> en;

int main() {
freopen("C-large.in", "r", stdin);
freopen("C-large.out", "w", stdout);

int t;
scanf("%d",&t);

for(int cnt=1; cnt<=t; ++cnt)
{
mp.clear();
en.clear();
scanf("%d",&n);
for(int i=0; i<n; ++i)
{
char a[30],b[30];
scanf("%s%s",a,b);
string x = a, y = b;

mp[x] = y;
en.insert(y);
}
string st,des;
for(auto p=mp.begin(); p!=mp.end(); ++p)
{
if(en.find(p->first)==en.end())
{
st = p->first;
break;
}
}

printf("Case #%d:",cnt);
for(; mp.find(st)!=mp.end(); st = des)
{
des = mp[st];
printf(" %s-%s",st.c_str(),des.c_str());
}
printf("\n");
}
}


#### Google中国2015校园招聘笔试Round D APAC Test Problem B. GBus count

2014-11-13 23:37:48

#### HDU3577：Fast Arrangement(线段树区间更新+lazy)

2013-11-07 20:29:13

#### FZU2271 X(最短路，Folyd变形)(第七届福建省大学生程序设计竞赛)

2017-07-18 21:36:14

#### 2017年上海金马五校程序设计竞赛：Problem C : Count the Number

2017-06-03 17:45:11

#### PAT 1131. Subway Map (30) -甲级

2017-03-31 14:49:28

#### Google APAC Test 2017 Round D

2016-10-19 22:36:34

#### 【在线笔试题解题报告系列】Google APAC 2017 University Test Round B

2016-08-28 16:29:17

2015-08-25 18:35:02

#### 【在线笔试题解题报告系列】Google APAC 2017 University Test Round A

2016-07-10 23:13:50

#### 京东2015年应届生招聘笔试题（A）卷答案选择题部分

2014-10-12 20:42:30