其实思路很简单,就是找到起点就可以了,那么就for一遍找到入度为0 的点之后,就可以开始输出了。
code:
#include <bits/stdc++.h>
using namespace std;
string name[10010];
int nex[10010];
int cnt;
bool vis[10010];
int main() {
int T;
cin>>T;
int a;
int xx = 1;
while (T --) {
cin>>a;
string x,y;
map<string,string> q;
set<string> p;
map<string,int> qaq;
for(int i = 1;i <= a;i ++) {
cin>>x>>y;
q[x] = y;
p.insert(x);
p.insert(y);
qaq[y] = 1;
}
string be;
for(const auto& i : p)
if (qaq[i] == 0)
be = i;
cout<<"Case #"<<xx<<": ";
xx ++;
while (!q[be].empty()) {
cout<<be<<"-"<<q[be]<<" ";
be = q[be];
}
cout<<"\n";
}
return 0;
}