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. remove(all(same[ppl[i]]), ppl[j]);
  64. }
  65. }
  66. }
  67. }
  68.  
  69.  
  70. for(auto i = 0; i < 3; i++) {
  71. if (no.find(ppl[i]) != no.end()) {
  72. for(auto j = 0; j < 3; j++) {
  73. if (i==j) {continue;}
  74.  
  75. if (find(all(no[ppl[i]]), ppl[j]) != no[ppl[i]].end()) {
  76. ans++;
  77. remove(all(no[ppl[i]]), ppl[j]);
  78. }
  79. }
  80. }
  81. }
  82. }
  83.  
  84. cout << ans << endl;
  85. }
  86.  
  87.  
  88. int32_t main()
  89. {
  90. ios_base::sync_with_stdio(false);
  91. cin.tie(NULL);
  92. solve();
  93. return 0;
  94. }
Success #stdin #stdout 0.01s 5472KB
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
12