Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- using namespace std;
- static const int N {100000};
- int main()
- {
- srand(time(NULL));
- int i;
- int j;
- int id[N];
- int sz[N]; // Stores tree sizes
- int Ncount{}; // Counts the numbeer of new connections
- int Mcount{}; // Counts the number of all attempted connections
- for (i = 0; i < N; i++)
- {
- id[i] = i;
- sz[i] = 1;
- }
- while (Ncount < N - 1)
- {
- i = rand() % N;
- j = rand() % N;
- for (; i != id[i]; i = id[i])
- id[i] = id[id[i]];
- for (; j != id[j]; j = id[j])
- id[j] = id[id[j]];
- Mcount++;
- if (i == j) // Checks if i and j are connected
- continue;
- if (sz[i] < sz[j]) // Smaller tree will be
- // connected to a bigger one
- {
- id[i] = j;
- sz[j] += sz[i];
- }
- else
- {
- id[j] = i;
- sz[i] += sz[j];
- }
- Ncount++;
- }
- cout << "Mcount: " << Mcount << endl;
- cout << "Ncount: " << Ncount << endl;
- return 0;
- }
- import random
- N = 100000
- idList = list(range(0, N))
- sz = [1] * N
- Ncount = 0
- Mcount = 0
- while Ncount < N - 1:
- i = random.randrange(0, N)
- j = random.randrange(0, N)
- while i is not idList[i]:
- idList[i] = idList[idList[i]]
- i = idList[i]
- while j is not idList[j]:
- idList[j] = idList[idList[j]]
- j = idList[j]
- Mcount += 1
- if i is j:
- continue
- if sz[i] < sz[j]:
- idList[i] = j
- sz[j] += sz[i]
- else:
- idList[j] = i
- sz[i] += sz[j]
- Ncount += 1
- print("Mcount: ", Mcount)
- print("Ncount: ", Ncount)
- >>> x=100000
- >>> y=100000
- >>> x is y
- False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement