当前位置:网站首页>693. Travel sequencing
693. Travel sequencing
2022-07-27 08:38:00 【Pursue people far away】
Mary needs to fly from one place to another , Because there is no direct flight , So we need to transfer many flights halfway .
for example :SFO -> DFW DFW -> JFK JFK -> MIA MIA -> ORD.
Obviously, it is impossible to travel to the same transit city twice or more , Because it doesn't make sense .
Unfortunately , She messed up the order of her tickets , It is not easy for her to arrange the tickets according to the order of travel .
Please help Mary arrange her ticket , Arrange the tickets in the correct order .
Input format
The first line contains integers T, Expressing common ownership T Group test data .
The first row of each set of data contains an integer N.
Next 2N That's ok , Every time 2 Row group , Indicates the information of a ticket , Each line contains a string , The first line indicates the place of departure , The second line indicates the destination .
Output format
Each group of data outputs a result , Each result takes up one line .
The result is expressed as Case #x: y, among xx It's the group number ( from 1 Start ),y Is a list of tickets representing the actual trip , Each leg of the journey should be marked with source-destination Form output , The segments are separated by spaces .
Data range
1≤T≤100,
1≤N≤10000
sample input :
2
1
SFO
DFW
4
MIA
ORD
DFW
JFK
SFO
DFW
JFK
MIA
sample output :
Case #1: SFO-DFW
Case #2: SFO-DFW DFW-JFK JFK-MIA MIA-ORD
Code :
#include <bits/stdc++.h>
using namespace std;
int main()
{
int T;
cin >> T;
for (int k = 1; k <= T; k++)
{
int n;
cin >> n;
unordered_map<string, string> next;
unordered_set<string> S;
while (n--)
{
string a, b;
cin >> a >> b;
next[a] = b;
S.insert(b);
}
string head;
for (auto &[a, b] : next)
if (!S.count(a))
{
head = a;
break;
}
printf("Case #%d: ", k);
while (next.count(head)) // Yes
{
cout << head << '-' << next[head] << ' ';
head = next[head];
}
cout << endl;
}
return 0;
}
边栏推荐
- Vcenter7.0 managing esxi7.0 hosts
- Flutter 渲染机制——GPU线程渲染
- Include error in vs Code (new header file)
- Flink1.15源码阅读flink-clients客户端执行流程(阅读较枯燥)
- All in one 1319 - queue for water
- What are the differences or similarities between "demand fulfillment to settlement" and "purchase to payment"?
- [penetration test tool sharing] [dnslog server building guidance]
- regular expression
- All in one 1329 cells (breadth first search)
- OSI seven layer model and tcp/ip four layer (TCP and UDP) (notes)
猜你喜欢
随机推荐
带宽 与 货币
Creation and simple application of QPushButton button button
Block, there is a gap between the block elements in the row
Cache consistency and memory barrier
Day3 -- flag state holding, exception handling and request hook
Flask one to many database creation, basic addition, deletion, modification and query
Flask's operations on model classes
Realize SKU management in the background
【渗透测试工具分享】【dnslog服务器搭建指导】
Help send some recruitment. If you are interested, you can have a look
P7 Day1 get to know the flask framework
Background image related applications - full, adaptive
After downloading URL loader and specifying the size of the image with limit, the image will not be displayed
Forced login, seven cattle cloud upload pictures
STM32 small bug summary
Iterators and generators
Pass parameters and returned responses of flask
What are the differences or similarities between "demand fulfillment to settlement" and "purchase to payment"?
[uni app advanced practice] take you hand-in-hand to learn the development of a purely practical complex project 1/100
Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering




![[netding cup 2020 rosefinch group]nmap 1 two solutions](/img/fa/b1349cb42b5768b7510217239ba73a.png)




