Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #ifdef DEBUG
  2. #define _GLIBCXX_DEBUG
  3. #endif
  4.  
  5. #include <bits/stdc++.h>
  6.  
  7. using namespace std;
  8.  
  9. mt19937 mrand(random_device{} ());
  10.  
  11. int rnd(int x) {
  12.   return mrand() % x;
  13. }
  14.  
  15. // -------------------------------------------------
  16.  
  17. const int maxn = (int) 1e7;
  18. int pr[maxn], w[maxn], ind[maxn];
  19.  
  20. int cntGet;
  21. int get(int v) {
  22.   ++cntGet;
  23.   return v == pr[v] ? v : (pr[v] = get(pr[v]));
  24. }
  25.  
  26. int TYPE;
  27.  
  28. void join(int s, int t) {
  29.   s = get(s), t = get(t);
  30.   if (s == t) {
  31.     return;
  32.   }
  33.   switch(TYPE) {
  34.     case 0:
  35.       if (rnd(2)) {
  36.         swap(s, t);
  37.       }
  38.       break;
  39.     case 1:
  40.       if (w[s] == w[t]) {
  41.         ++w[s];
  42.       } else {
  43.         if (w[s] < w[t]) {
  44.           swap(s, t);
  45.         }
  46.       }
  47.       break;
  48.     case 2:
  49.       if (ind[s] < ind[t]) {
  50.         swap(s, t);
  51.       }
  52.       break;
  53.     default:
  54.       assert(0);
  55.   }
  56.  
  57.   pr[t] = s;
  58. }
  59.  
  60. double solve(int n) {
  61.   for (int i = 0; i < n; ++i) {
  62.     pr[i] = i, w[i] = 0, ind[i] = i;
  63.   }
  64.  
  65.   random_shuffle(ind, ind + n, rnd);
  66.  
  67.   cntGet = 0;
  68.   for (int i = 1; i < n; ++i) {
  69.     join(0, rnd(i / sqrt(5.0) + 1));
  70.   }
  71.  
  72.   return (double) cntGet / n;
  73. }
  74.  
  75. int main() {
  76.   for (TYPE = 0; TYPE < 3; ++TYPE) {
  77.     printf(!TYPE ? "RANDOM:\n" : (TYPE == 1 ? "RANKS:\n" : "INDEXES:\n"));
  78.     for (int n = 1e4; n <= maxn; n *= 10) {
  79.       printf("res[%.1e] = %.3f\n", (double) n, solve(n));
  80.     }
  81.   }
  82.   return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement