fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include<unordered_map>
  4. #include <string>
  5. using namespace std;
  6. #include<algorithm>
  7. #include<set>
  8.  
  9. #define Fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  10. #define ll long long
  11.  
  12.  
  13. vector<pair<string, string>>ans;
  14.  
  15. unordered_map<string, string>mp;
  16. set<string>st;
  17. string find1(string second) {
  18. string name = second;
  19.  
  20. auto it = mp.find(second);
  21. if (it != mp.end()) {
  22.  
  23. name = find1(it->second);
  24.  
  25. }
  26. return name;
  27. }
  28. void sol() {
  29.  
  30. int n; cin >> n;
  31.  
  32. while (n--) {
  33. string old, newname; cin >> old >> newname; mp.insert({ old,newname });
  34. }
  35. for (auto it : mp) {
  36. if (st.find(it.first) == st.end()) {
  37. ans.push_back({ it.first, find1(it.second) });
  38.  
  39. }
  40. }
  41. cout << ans.size() << endl;
  42. reverse(ans.begin(), ans.end());
  43. for (auto it : ans) {
  44. cout << it.first << " " << it.second << endl;
  45. }
  46.  
  47. }
  48.  
  49. int main() {
  50. Fast
  51. int t = 1;
  52. while (t--) {
  53. sol();
  54. }
  55. return 0;
  56. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
0