Advertisement
Gosunov

Untitled

Nov 25th, 2021
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #include <chrono>
  2. #include <iostream>
  3. #include <random>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. #define int long long
  9.  
  10. int seed = chrono::high_resolution_clock::now().time_since_epoch().count();
  11. mt19937 mt(seed);
  12.  
  13. vector<int> p, height;
  14.  
  15. int get(int x) {
  16.     if (p[x] == x) return x;
  17.     return p[x] = get(p[x]);
  18. }
  19.  
  20. void join(int x, int y) {
  21.     x = get(x), y = get(y);
  22.     if (x != y) {
  23.         if (height[x] < height[y]) swap(x, y);
  24.         p[y] = x;
  25.         if (height[x] == height[y])
  26.             height[x]++;
  27.     }
  28. }
  29.  
  30. bool is_connected(int x, int y) {
  31.     return get(x) == get(y);
  32. }
  33.  
  34. int rnd(int a, int b) {
  35.     return a + mt() % (b - a + 1);
  36. }
  37.  
  38. signed main() {
  39.  
  40.     const int n = 40;
  41.     cout << n << '\n';
  42.     int Data[n];
  43.  
  44.     p.resize(n);
  45.     height.resize(n, 1);
  46.     for (int i = 0; i < n; i++) p[i] = i;
  47.  
  48.     //cout << is_connected(0,2) << endl;
  49.  
  50.     for(int i = 1; i < n; i++){
  51.         vector<int> able;
  52.         for(int j =0;j<n;j++){
  53.             if (i != j){
  54.                 if ( !is_connected(i,j)){
  55.                     able.push_back(j);
  56.  
  57.                 }
  58.             }
  59.         }
  60.         int rand_connect = able[rnd(0,able.size()-1)];
  61.         Data[i] = rand_connect;
  62.         join(i,rand_connect);
  63.     }
  64.  
  65.     for(int i =1;i<n;i++){
  66.         cout << Data[i]+1 << " ";
  67.     }
  68.     cout << '\n';
  69.  
  70.     //
  71.     p.clear();
  72.     height.clear();
  73.     p.resize(n);
  74.     height.resize(n, 1);
  75.     for (int i = 0; i < n; i++) p[i] = i;
  76.  
  77.     for(int i = 1; i < n; i++){
  78.         vector<int> able;
  79.         for(int j =0;j<n;j++){
  80.             if (i != j){
  81.                 if ( !is_connected(i,j)){
  82.                     able.push_back(j);
  83.  
  84.                 }
  85.             }
  86.         }
  87.         int rand_connect = able[rnd(0,able.size()-1)];
  88.         Data[i] = rand_connect;
  89.         join(i,rand_connect);
  90.     }
  91.  
  92.     for(int i =1;i<n;i++){
  93.         cout << Data[i]+1 << " ";
  94.     }
  95.     cout << '\n';
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement