fork download
  1. #include <bits/stdc++.h>
  2. #include <fstream>
  3. using namespace std;
  4. #define read(type) readInt<type>() // Fast read
  5. #define ll long long
  6. #define nL "\n"
  7. #define pb push_back
  8. #define mk make_pair
  9. #define pii pair<int, int>
  10. #define a first
  11. #define b second
  12. #define vi vector<int>
  13. #define all(x) (x).begin(), (x).end()
  14. #define umap unordered_map
  15. #define uset unordered_set
  16. #define MOD 1000000007
  17. #define imax INT_MAX
  18. #define imin INT_MIN
  19. #define exp 1e9
  20. #define sz(x) (int((x).size()))
  21.  
  22. void solve() {
  23. int x; cin >> x;
  24. map<string, vector<string>> same;
  25.  
  26. for(auto i = 0; i < x; i++) {
  27. string o, p; cin >> o >> p;
  28. if (same.find(o) == same.end()) {
  29. same[o] = {p};
  30. } else {same[o].pb(p);}
  31.  
  32. if (same.find(p) == same.end()) {
  33. same[p] = {o};
  34. } else {same[p].pb(o);}
  35. }
  36.  
  37. int y; cin >> y;
  38. map<string, vector<string>> no;
  39.  
  40. for(auto i = 0; i < y; i++) {
  41. string o, p; cin >> o >> p;
  42. if (no.find(o) == no.end()) {
  43. no[o] = {p};
  44. } else {no[o].pb(p);}
  45.  
  46. if (no.find(p) == no.end()) {
  47. no[p] = {o};
  48. } else {no[p].pb(o);}
  49. }
  50.  
  51. for(auto const& x : same) {
  52. cout << "Key: " << x.a << " Val: ";
  53. for(auto v : x.b) {
  54. cout << v << " ";
  55. }
  56. cout << endl;
  57. }
  58.  
  59. int ans = 0;
  60. int q; cin >> q;
  61. while(q--) {
  62. string z, v, b; cin >> z >> v >> b;
  63. vector<string> ppl = {z, v, b};
  64.  
  65. for(auto i = 0; i < 3; i++) {
  66. if (same.find(ppl[i]) != same.end()) {
  67. for(auto j = 0; j < 3; j++) {
  68. if (i == j) {continue;}
  69. if (find(all(same[ppl[i]]), ppl[j]) != same[ppl[i]].end()) {
  70. ans++;
  71. cout << "Found " << ppl[i] << " and " << ppl[j] << endl;
  72. remove(all(same[ppl[i]]), ppl[j]);
  73. remove(all(same[ppl[j]]), ppl[i]);
  74. }
  75. }
  76. }
  77. }
  78.  
  79.  
  80. // for(auto i = 0; i < 3; i++) {
  81. // if (no.find(ppl[i]) != no.end()) {
  82. // for(auto j = 0; j < 3; j++) {
  83. // if (i==j) {continue;}
  84.  
  85. // if (find(all(no[ppl[i]]), ppl[j]) != no[ppl[i]].end()) {
  86. // ans++;
  87. // remove(all(no[ppl[i]]), ppl[j]);
  88. // remove(all(no[ppl[j]]), ppl[i]);
  89. // }
  90. // }
  91. // }
  92. // }
  93. }
  94.  
  95. cout << ans << endl;
  96. }
  97.  
  98.  
  99. int32_t main()
  100. {
  101. ios_base::sync_with_stdio(false);
  102. cin.tie(NULL);
  103. solve();
  104. return 0;
  105. }
Success #stdin #stdout 0s 5512KB
stdin
3
A B
G L
J K
2
D F
D G
4
A C G
B D F
E H I
J K L
stdout
Key: A Val: B 
Key: B Val: A 
Key: G Val: L 
Key: J Val: K 
Key: K Val: J 
Key: L Val: G 
Found J and K
Found K and J
2