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. int ans = 0;
  52. int q; cin >> q;
  53. while(q--) {
  54. string z, v, b; cin >> z >> v >> b;
  55. vector<string> ppl = {z, v, b};
  56.  
  57. for(auto i = 0; i < 3; i++) {
  58. if (same.find(ppl[i]) != same.end()) {
  59. for(auto j = 0; j < 3; j++) {
  60. if (i == j) {continue;}
  61. if (find(all(same[ppl[i]]), ppl[j]) == same[ppl[i]].end()) {
  62. ans++;
  63. cout << "Found " << ppl[i] << " and " << ppl[j] << endl;
  64. remove(all(same[ppl[i]]), ppl[j]);
  65. remove(all(same[ppl[j]]), ppl[i]);
  66. }
  67. }
  68. }
  69. }
  70.  
  71.  
  72. // for(auto i = 0; i < 3; i++) {
  73. // if (no.find(ppl[i]) != no.end()) {
  74. // for(auto j = 0; j < 3; j++) {
  75. // if (i==j) {continue;}
  76.  
  77. // if (find(all(no[ppl[i]]), ppl[j]) != no[ppl[i]].end()) {
  78. // ans++;
  79. // remove(all(no[ppl[i]]), ppl[j]);
  80. // remove(all(no[ppl[j]]), ppl[i]);
  81. // }
  82. // }
  83. // }
  84. // }
  85. }
  86.  
  87. cout << ans << endl;
  88. }
  89.  
  90.  
  91. int32_t main()
  92. {
  93. ios_base::sync_with_stdio(false);
  94. cin.tie(NULL);
  95. solve();
  96. return 0;
  97. }
Success #stdin #stdout 0.01s 5296KB
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
Found A and C
Found A and G
Found C and A
Found C and G
Found G and A
Found G and C
Found B and D
Found B and F
Found D and B
Found D and F
Found F and B
Found F and D
Found J and L
Found K and L
Found L and J
Found L and K
16