# 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 A. Cube IV

Problem Vincenzo decides to make cube IV but only has the budget to make a square maze. Its a perfe...
• maverick1990
• 2014年11月13日 23:28
• 1151

## Google中国2015校园招聘笔试Round D APAC Test Problem D. Itz Chess

Problem Given an arranged chess board with pieces, figure out the total number of different ways in...
• maverick1990
• 2014年11月14日 14:50
• 994

• kk303
• 2015年11月10日 09:33
• 704

## Catalan数和应用 & 2015 google APAC round 2 problem D 括号配对问题

Catalan数是排列组合中很重要的一种数
• xiaonimao
• 2014年09月30日 20:31
• 571

## 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 ...
• maverick1990
• 2014年11月14日 11:44
• 1186

## Google APAC 2017 University Test Round E

Google APAC 2017 University Test Round E
• u012734359
• 2016年11月06日 16:24
• 730

• sinat_17277843
• 2015年09月01日 16:05
• 68

## CODE JAM APAC 2015 round B: Problem A. Password Attacker（小球放进盒子的计数问题）

• xiaonimao
• 2014年09月17日 13:18
• 1033

## Round A APAC Test 2017 java完整代码及解答

• u112222222
• 2016年08月14日 21:45
• 654

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

Problem There exists a straight line along which cities are built. Each city is given a number s...
• maverick1990
• 2014年11月13日 23:37
• 787

举报原因： 您举报文章：Google中国2015校园招聘笔试Round D APAC Test Problem C. Sort a scrambled itinerary 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)