Advertisement
mickypinata

SMMR-T248: Battle Royale

Jul 3rd, 2021
1,139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long lli;
  5.  
  6. const int N = 2e5;
  7. const int PB = 1e9 + 7;
  8.  
  9. map<lli, int> nameToIdx;
  10. char *name[N + 1];
  11. int ds[N + 1], sz[N + 1], lessThan[N + 1], mx[N + 1];
  12.  
  13. lli strHash(char *str){
  14.     lli hsh = 0;
  15.     int len = strlen(str);
  16.     for(int i = 0; i < len; ++i){
  17.         hsh *= PB;
  18.         hsh += str[i];
  19.     }
  20.     return hsh;
  21. }
  22.  
  23. int currIdx = 1;
  24. int addName(char *str){
  25.     lli hsh = strHash(str);
  26.     map<lli, int>::iterator itr = nameToIdx.find(hsh);
  27.     if(itr == nameToIdx.end()){
  28.         nameToIdx[hsh] = currIdx;
  29.         name[currIdx] = new char[11];
  30.         strcpy(name[currIdx], str);
  31.         return currIdx++;
  32.     }
  33.     return itr -> second;
  34. }
  35.  
  36. int dsFind(int u){
  37.     if(ds[u] == u){
  38.         return u;
  39.     }
  40.     int p = dsFind(ds[u]);
  41.     lessThan[u] += lessThan[ds[u]];
  42.     return ds[u] = p;
  43. }
  44.  
  45. int main(){
  46.  
  47.     int nRobot, Q;
  48.     scanf("%d%d", &nRobot, &Q);
  49.     // DSU Setup
  50.     for(int i = 1; i <= nRobot; ++i){
  51.         ds[i] = i;
  52.         sz[i] = 1;
  53.         lessThan[i] = 0;
  54.         mx[i] = i;
  55.     }
  56.     int setCnt = nRobot;
  57.     while(Q--){
  58.         char *strA = new char[11];
  59.         char *strB = new char[11];
  60.         int cmd;
  61.         scanf("%d", &cmd);
  62.         if(cmd == 1){ // Union
  63.             int stronger;
  64.             scanf("%s %s%d", strA, strB, &stronger);
  65.             int u = addName(strA);
  66.             int v = addName(strB);
  67.             int headU = dsFind(u);
  68.             int headV = dsFind(v);
  69.             if(headU != headV){
  70.                 if(sz[headU] < sz[headV]){
  71.                     swap(headU, headV);
  72.                     swap(u, v);
  73.                     stronger *= -1;
  74.                 }
  75.                 ds[headV] = ds[headU];
  76.                 sz[headU] += sz[headV];
  77.                 lessThan[headV] = stronger + lessThan[u] - lessThan[v];
  78.                 --setCnt;
  79.                 dsFind(mx[headV]);
  80.                 if(lessThan[mx[headV]] < lessThan[mx[headU]]){
  81.                     mx[headU] = mx[headV];
  82.                 }
  83.             }
  84.         } else if(cmd == 2){ // u > v
  85.             scanf("%s %s", strA, strB);
  86.             int u = addName(strA);
  87.             int v = addName(strB);
  88.             if(dsFind(u) == dsFind(v)){
  89.                 cout << lessThan[v] - lessThan[u] << '\n';
  90.             } else {
  91.                 cout << "cannot tell\n";
  92.             }
  93.         } else if(cmd == 3){ // Max of All
  94.             if(setCnt == 1){
  95.                 printf("%s\n", name[mx[dsFind(1)]]);
  96.             } else {
  97.                 cout << "cannot tell\n";
  98.             }
  99.         }
  100.     }
  101.  
  102.     return 0;
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement