AcWing 4303. 链表
原题链接
中等
作者:
沈子楚
,
2023-08-04 22:42:47
,
所有人可见
,
阅读 64
//用map做
#include<bits/stdc++.h>
#include <unordered_map>
using namespace std;
typedef long long ll;
int q;
vector<pair<string, string>> g, res;
unordered_map<string, string> mp;
int main(){
cin >> q;
while(q --){
string a, b;
cin >> a >> b;
g.push_back({a, b});
mp[a] = b;
}
for(int i = 0; i < g.size(); i ++){
string s = g[i].second;
if(mp.count(g[i].first)){
while(mp.count(s)){
string temp = s;
s = mp[s];
mp.erase(temp);
}
res.push_back({g[i].first, s});
}
}
cout << res.size() << endl;
for(auto e : res){
cout << e.first << ' ' << e.second << endl;
}
return 0;
}