当前位置:网站首页>693. Travel sequencing (map + topology)
693. Travel sequencing (map + topology)
2022-07-02 13:45:00 【Joanh_ Lan】
The title is as follows
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 row lists 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 x 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
Ideas :
map + Topology
AC The code is as follows :
#include <bits/stdc++.h>
#define buff \ ios::sync_with_stdio(false); \ cin.tie(0);
//#define int long long
using namespace std;
const int N = 10009;
int d[N];
int n;
map<string, int> mp;
int h[N], e[N], ne[N], idx;
int cnt;
string ch[N];
int rou;
void add(int a, int b)
{
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
void topsort()
{
int q[N];
int tt = -1, hh = 0;
for (int i = 1; i <= cnt; i++)
if (!d[i])
{
q[++tt] = i;
break;
}
while (hh <= tt)
{
int t = q[hh++];
for (int i = h[t]; i != -1; i = ne[i])
{
int j = e[i];
d[j]--;
if (!d[j])
q[++tt] = j;
}
}
for (int i = 0; i < cnt - 1; i++)
cout << ch[q[i]] << '-' << ch[q[i + 1]] << ' ';
}
void solve()
{
rou++;
mp.clear();
memset(d, 0, sizeof d);
memset(h, -1, sizeof h);
cnt = 0, idx = 0;
cin >> n;
for (int i = 1; i <= n; i++)
{
string a, b;
cin >> a >> b;
if (!mp[a])
{
mp[a] = ++cnt;
ch[cnt] = a;
}
if (!mp[b])
{
mp[b] = ++cnt;
ch[cnt] = b;
}
d[mp[b]]++;
add(mp[a], mp[b]);
}
cout << "Case #" << rou << ": ";
topsort();
cout << '\n';
}
int main()
{
int t;
cin >> t;
while (t--)
solve();
}
边栏推荐
- [USACO05JAN]Watchcow S(欧拉回路)
- OpenFOAM:lduMatrix&lduAddressing
- 题解:《你的飞碟在这儿》、《哥德巴赫猜想》
- Three methods of finding LCA of the nearest common ancestor
- 693. 行程排序(map + 拓扑)
- JS reverse massive creative signature
- 使用BLoC 构建 Flutter的页面实例
- Sum of the first n terms of Fibonacci (fast power of matrix)
- 诚邀青年创作者,一起在元宇宙里与投资人、创业者交流人生如何做选择……...
- Three talking about exception -- error handling
猜你喜欢
[indomitable medal activity] life goes on and writing goes on
Qt入门-制作一个简易的计算器
Pointer from entry to advanced (1)
Find love for speed in F1 delta time Grand Prix
De4000h storage installation configuration
Qt-制作一个简单的计算器-实现四则运算
ensp简单入门
Countermeasures for the failure of MMPV billing period caused by negative inventory of materials in SAP mm
OpenFOAM:lduMatrix&lduAddressing
Solve "sub number integer", "jump happily", "turn on the light"
随机推荐
D language, possible 'string plug-ins'
Why can't d link DLL
Android kotlin material design technology points
mysql ---- Oracle中的rownum转换成MySQL
Runhe hi3516 development board openharmony small system and standard system burning
[true topic of the Blue Bridge Cup trials 43] scratch space flight children's programming explanation of the true topic of the Blue Bridge Cup trials
【模板】最长公共子序列 (【DP or 贪心】板子)
MAC (MacOS Monterey 12.2 M1) personal use PHP development
JS逆向之行行查data解密
你的 Sleep 服务会梦到服务网格外的 bookinfo 吗
文件的下载与图片的预览
[USACO05JAN]Watchcow S(欧拉回路)
D为何链接不了dll
Android kotlin broadcast technology point
P3807 [template] Lucas theorem /lucas theorem
Android kotlin fragment technology point
Node. JS accessing PostgreSQL database through ODBC
研究表明“气味相投”更易成为朋友
[Blue Bridge Cup] children's worship circle
[OpenGL] notes 29. Advanced lighting (specular highlights)