Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3.  
  4. using namespace std;
  5.  
  6. static const int N {100000};
  7.  
  8. int main()
  9. {
  10. srand(time(NULL));
  11.  
  12. int i;
  13. int j;
  14.  
  15. int id[N];
  16. int sz[N]; // Stores tree sizes
  17.  
  18. int Ncount{}; // Counts the numbeer of new connections
  19. int Mcount{}; // Counts the number of all attempted connections
  20.  
  21. for (i = 0; i < N; i++)
  22. {
  23. id[i] = i;
  24. sz[i] = 1;
  25. }
  26.  
  27. while (Ncount < N - 1)
  28. {
  29. i = rand() % N;
  30. j = rand() % N;
  31.  
  32. for (; i != id[i]; i = id[i])
  33. id[i] = id[id[i]];
  34.  
  35. for (; j != id[j]; j = id[j])
  36. id[j] = id[id[j]];
  37.  
  38. Mcount++;
  39.  
  40. if (i == j) // Checks if i and j are connected
  41. continue;
  42.  
  43. if (sz[i] < sz[j]) // Smaller tree will be
  44. // connected to a bigger one
  45. {
  46. id[i] = j;
  47. sz[j] += sz[i];
  48. }
  49. else
  50. {
  51. id[j] = i;
  52. sz[i] += sz[j];
  53. }
  54.  
  55. Ncount++;
  56. }
  57.  
  58. cout << "Mcount: " << Mcount << endl;
  59. cout << "Ncount: " << Ncount << endl;
  60.  
  61. return 0;
  62. }
  63.  
  64. import random
  65.  
  66. N = 100000
  67.  
  68. idList = list(range(0, N))
  69. sz = [1] * N
  70.  
  71. Ncount = 0
  72. Mcount = 0
  73.  
  74. while Ncount < N - 1:
  75.  
  76. i = random.randrange(0, N)
  77. j = random.randrange(0, N)
  78.  
  79. while i is not idList[i]:
  80. idList[i] = idList[idList[i]]
  81. i = idList[i]
  82.  
  83. while j is not idList[j]:
  84. idList[j] = idList[idList[j]]
  85. j = idList[j]
  86.  
  87. Mcount += 1
  88.  
  89. if i is j:
  90. continue
  91.  
  92. if sz[i] < sz[j]:
  93. idList[i] = j
  94. sz[j] += sz[i]
  95. else:
  96. idList[j] = i
  97. sz[i] += sz[j]
  98.  
  99. Ncount += 1
  100.  
  101. print("Mcount: ", Mcount)
  102. print("Ncount: ", Ncount)
  103.  
  104. >>> x=100000
  105. >>> y=100000
  106. >>> x is y
  107. False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement