当前位置:网站首页>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();
}
边栏推荐
- A better database client management tool than Navicat
- TVOC, VOC, VOCs gas detection + Solution
- 验证失败,请检查您的回电网址。您可以按照指导进行操作
- EasyDSS点播服务分享时间出错如何修改?
- [USACO05JAN]Watchcow S(欧拉回路)
- Nohup command
- Explanation: here is your UFO, Goldbach conjecture
- The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
- 中文姓名提取(玩具代码——准头太小,权当玩闹)
- 操作教程:EasyDSS如何将MP4点播文件转化成RTSP视频流?
猜你喜欢

Daily practice of C language --- monkeys divide peaches

Qt-制作一个简单的计算器-实现四则运算

Runhe hi3516 development board openharmony small system and standard system burning

记忆函数的性能优化

错误:EACCES:权限被拒绝,访问“/usr/lib/node_modules”

Drawing Nyquist diagram with MATLAB

Qt新项目_MyNotepad++

EasyDSS点播服务分享时间出错如何修改?

Performance optimization of memory function

The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
随机推荐
JS reverse massive creative signature
Redis数据库持久化
JS逆向之行行查data解密
Astro learning notes
(POJ - 1984) navigation nightare (weighted and search set)
Winter vacation daily question - lucky numbers in the matrix
How to modify the error of easydss on demand service sharing time?
OpenFOAM:lduMatrix&lduAddressing
mysql ---- Oracle中的rownum转换成MySQL
石子合并板子【区间DP】(普通石子合并 & 环形石子合并)
Independent and controllable 3D cloud CAD: crowncad enables innovative design of enterprises
OpenApi-Generator:简化RESTful API开发流程
机器学习基础(二)——训练集和测试集的划分
linux下清理系统缓存并释放内存
为什么switch 的default后面要跟break?
What are the classifications of SSL certificates? How to choose the appropriate SSL certificate?
TVOC, VOC, VOCs gas detection + Solution
I did it with two lines of code. As a result, my sister had a more ingenious way
不会看器件手册的工程师不是个好厨子
操作教程:EasyDSS如何将MP4点播文件转化成RTSP视频流?