当前位置:网站首页>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();
}
边栏推荐
- JS逆向之行行查data解密
- [技术发展-22]:网络与通信技术的应用与发展快速概览-2- 通信技术
- Japan bet on national luck: Web3.0, anyway, is not the first time to fail!
- The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
- Engineers who can't read device manuals are not good cooks
- SAP MM 因物料有负库存导致MMPV开账期失败问题之对策
- We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse
- [USACO05JAN]Watchcow S(欧拉回路)
- Drawing Nyquist diagram with MATLAB
- Explanation: here is your UFO, Goldbach conjecture
猜你喜欢

大家信夫一站式信用平台让信用场景“用起来

无向图的桥

Can automatically update the universal weekly report template, you can use it with your hand!

Qt-制作一个简单的计算器-实现四则运算-将结果以对话框的形式弹出来

【笔耕不辍勋章活动】生命不止,写作不息

Daily practice of C language --- monkeys divide peaches

代码实现MNLM

Unity skframework framework (XII), score scoring module

OpenApi-Generator:简化RESTful API开发流程

Unity skframework framework (XIX), POI points of interest / information points
随机推荐
I did it with two lines of code. As a result, my sister had a more ingenious way
How to use SAP's metadata framework (MDF) to build custom business rules?
Student course selection information management system based on ssm+jsp framework [source code + database]
D如何检查null
JS逆向之巨量创意signature签名
Three methods of finding LCA of the nearest common ancestor
基于ssm+jsp框架实现的学生选课信息管理系统【源码+数据库】
2022零代码/低代码开发白皮书【伙伴云出品】附下载
Independent and controllable 3D cloud CAD: crowncad enables innovative design of enterprises
Pocket Raider comments
题解:《你的飞碟在这儿》、《哥德巴赫猜想》
石子合并板子【区间DP】(普通石子合并 & 环形石子合并)
Research shows that "congenial" is more likely to become friends
每日一题:1175.质数排列
Web基础
解答:EasyDSS视频点播时音频是否可以设置为默认开启?
ensp简单入门
The 29 year old programmer in Shanghai was sentenced to 10 months for "deleting the database and running away" on the day of his resignation!
Pointer from entry to advanced (1)
OpenAPI generator: simplify the restful API development process