Advertisement
YEZAELP

SMMR-248: Battle Royale

Jun 30th, 2021 (edited)
698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. unordered_map <string, vector <string>> member;
  5. unordered_map <string, string> par;
  6. unordered_map <string, int> val;
  7. set <string> have;
  8. const int INF = 2e9;
  9. int n, Q;
  10. bool found = false;
  11. string mx;
  12.  
  13. void Ins(string str){
  14.     if(have.find(str) != have.end()) return;
  15.     val[str] = 0;
  16.     par[str] = str;
  17.     member[str].push_back(str);
  18.     have.insert(str);
  19. }
  20.  
  21. void Merge(string u, string v, int x){ /// u <- v
  22.     par[v] = u;
  23.     for(auto i: member[v]){
  24.         member[u].push_back(i);
  25.         par[i] = u;
  26.         val[i] += x;
  27.     }
  28.     member[v].clear();
  29.     if(member[u].size() == n) {
  30.         mx = u;
  31.         found = true;
  32.     }
  33. }
  34.  
  35. void Combine(string a, string b, int x){
  36.     Ins(a);
  37.     Ins(b);
  38.     if(par[a] == par[b]) return;
  39.     /// a >= b
  40.     if(x < 0) swap(a, b);
  41.     x = abs(x);
  42.     if(val[b] > val[a] + x)
  43.         Merge(par[b], par[a], val[b] - val[a] - x);
  44.     else
  45.         Merge(par[a], par[b], val[a] + x - val[b]);
  46. }
  47.  
  48. int Query(string a, string b){
  49.     if(par[a] != par[b]
  50.     or have.find(a) == have.end()
  51.     or have.find(b) == have.end())
  52.         return INF;
  53.     return val[b] - val[a];
  54. }
  55.  
  56. int main(){
  57.  
  58.     scanf("%d%d", &n, &Q);
  59.  
  60.     while(Q--){
  61.         int opr, x;
  62.         string a, b;
  63.         scanf("%d", &opr);
  64.         if(opr == 1){
  65.             cin >> a >> b;
  66.             scanf("%d", &x);
  67.             Combine(a, b, x);
  68.         }
  69.         else if(opr == 2){
  70.             cin >> a >> b;
  71.             int q = Query(a, b);
  72.             if(q != INF) printf("%d\n", q);
  73.             else printf("cannot tell\n");
  74.         }
  75.         else if(opr == 3){
  76.             if(found) cout << mx << '\n';
  77.             else printf("cannot tell\n");
  78.         }
  79.     }
  80.  
  81.     return 0;
  82. }
  83.  
  84. /*
  85.  
  86. 8 16
  87. 1 1 2 20
  88. 1 3 4 5
  89. 1 4 1 -55
  90. 1 5 6 -30
  91. 1 5 7 10
  92. 2 2 6
  93. 2 2 3
  94. 2 6 7
  95. 3
  96. 1 6 7 40
  97. 1 7 1 -70
  98. 2 5 1
  99. 2 7 8
  100. 3
  101. 1 7 8 20
  102. 3
  103.  
  104. 7 12
  105. 1 3 1 2
  106. 1 6 7 -1
  107. 1 4 7 -3
  108. 1 7 3 4
  109. 1 2 5 3
  110. 1 2 1 1
  111. 3
  112.  
  113. 10 20
  114. 1 3 5 -2
  115. 1 7 5 2
  116. 1 2 3 -1
  117. 1 5 4 1
  118. 2 2 7
  119. 2 4 7
  120. 2 7 3
  121. 1 8 9 -1
  122. 1 10 9 1
  123. 1 6 8 -2
  124. 1 6 1 5
  125. 3
  126. 2 1 10
  127. 2 8 1
  128. 1 3 6 -3
  129. 3
  130. 2 4 10
  131. 2 7 8
  132. 2 6 9
  133.  
  134. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement