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. bool found = false;
  68. for(auto j = 0; j < 3; j++) {
  69. if (i == j) {continue;}
  70. if (find(all(same[ppl[i]]), ppl[j]) != same[ppl[i]].end()) {
  71. found=true;
  72. }
  73.  
  74. if (!found) {
  75. ans++;
  76. remove(all(same[ppl[i]]), ppl[j]);
  77. remove(all(same[ppl[j]]), ppl[i]);
  78. }
  79. }
  80. }
  81. }
  82.  
  83.  
  84. // for(auto i = 0; i < 3; i++) {
  85. // if (no.find(ppl[i]) != no.end()) {
  86. // for(auto j = 0; j < 3; j++) {
  87. // if (i==j) {continue;}
  88.  
  89. // if (find(all(no[ppl[i]]), ppl[j]) != no[ppl[i]].end()) {
  90. // ans++;
  91. // remove(all(no[ppl[i]]), ppl[j]);
  92. // remove(all(no[ppl[j]]), ppl[i]);
  93. // }
  94. // }
  95. // }
  96. // }
  97. }
  98.  
  99. cout << ans << endl;
  100. }
  101.  
  102.  
  103. int32_t main()
  104. {
  105. ios_base::sync_with_stdio(false);
  106. cin.tie(NULL);
  107. solve();
  108. return 0;
  109. }
Success #stdin #stdout 0.01s 5300KB
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 
14