Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <chrono>
- #include <iostream>
- #include <random>
- #include <algorithm>
- using namespace std;
- #define int long long
- int seed = chrono::high_resolution_clock::now().time_since_epoch().count();
- mt19937 mt(seed);
- vector<int> p, height;
- int get(int x) {
- if (p[x] == x) return x;
- return p[x] = get(p[x]);
- }
- void join(int x, int y) {
- x = get(x), y = get(y);
- if (x != y) {
- if (height[x] < height[y]) swap(x, y);
- p[y] = x;
- if (height[x] == height[y])
- height[x]++;
- }
- }
- bool is_connected(int x, int y) {
- return get(x) == get(y);
- }
- int rnd(int a, int b) {
- return a + mt() % (b - a + 1);
- }
- signed main() {
- const int n = 40;
- cout << n << '\n';
- int Data[n];
- p.resize(n);
- height.resize(n, 1);
- for (int i = 0; i < n; i++) p[i] = i;
- //cout << is_connected(0,2) << endl;
- for(int i = 1; i < n; i++){
- vector<int> able;
- for(int j =0;j<n;j++){
- if (i != j){
- if ( !is_connected(i,j)){
- able.push_back(j);
- }
- }
- }
- int rand_connect = able[rnd(0,able.size()-1)];
- Data[i] = rand_connect;
- join(i,rand_connect);
- }
- for(int i =1;i<n;i++){
- cout << Data[i]+1 << " ";
- }
- cout << '\n';
- //
- p.clear();
- height.clear();
- p.resize(n);
- height.resize(n, 1);
- for (int i = 0; i < n; i++) p[i] = i;
- for(int i = 1; i < n; i++){
- vector<int> able;
- for(int j =0;j<n;j++){
- if (i != j){
- if ( !is_connected(i,j)){
- able.push_back(j);
- }
- }
- }
- int rand_connect = able[rnd(0,able.size()-1)];
- Data[i] = rand_connect;
- join(i,rand_connect);
- }
- for(int i =1;i<n;i++){
- cout << Data[i]+1 << " ";
- }
- cout << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement