#include <iostream>
#include <unordered_map>
using namespace std;
const int N = 100010;
int t, n;
int main()
{
cin >> t;
for (int c = 1; c <= t; c ++ ) // c: 组别编号
{
unordered_map<string, int> cnt; // cnt: 城市被到达的次数
unordered_map<string, string> nxt; // nxt: 存出发地与目的地,nxt[出发地] = 目的地
cin >> n;
for (int i = 0; i < n; i ++ )
{
string a, b;
cin >> a >> b, cnt[a] ++ , cnt[b] ++ , nxt[a] = b;
}
string s; // s: 旅途的出发点
for (auto [k, v] : cnt) // k: 出发地,v: 目的地
if (v == 1 && nxt.count(k)) // 只出现了一次并且是出发地
s = k;
// 输出
cout << "Case #" << c << ": ";
for (string i = s; nxt.count(i); i = nxt[i]) cout << i << '-' << nxt[i] << ' ';
cout << '\n';
}
return 0;
}
作者:WangJY
链接:https://www.acwing.com/solution/content/122428/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
693. 行程排序
最新推荐文章于 2024-07-25 11:27:31 发布