Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <ctime>
  5. using namespace std;
  6. const int n = 1000000, m = 2000000;
  7. int a[m],b[m],i,p1[n],p2[n],size[n];
  8. double start, finish;
  9. int Find2(int v){
  10. if(v == p2[v])
  11. return v;
  12. return p2[v] = Find2(p2[v]);
  13. }
  14. void Union2(int a, int b){
  15. a = Find2(a);
  16. b = Find2(b);
  17. if(a == b)
  18. return;
  19. if(size[a] < size[b])
  20. swap(a, b);
  21. size[a] += size[b];
  22. p2[b] = a;
  23. }
  24. int Find1(int v){
  25. if(v == p1[v])
  26. return v;
  27. return p1[v] = Find1(p1[v]);
  28. }
  29. void Union1(int a, int b){
  30. a = Find1(a);
  31. b = Find1(b);
  32. if(a == b)
  33. return;
  34. if(rand() % 2)
  35. swap(a, b);
  36. p1[b] = a;
  37. }
  38. int main(){
  39. freopen("input.txt","r",stdin);
  40. ios_base :: sync_with_stdio(0);
  41. cin.tie();
  42. srand(time(0));
  43. for(i = 0; i < m; i++){
  44. a[i] = (rand() + (rand() << 15)) % n;
  45. b[i] = (rand() + (rand() << 15)) % n;
  46. //cout << a[i] << " " << b[i] << endl;
  47. }
  48. for(i = 0; i < n; i++){
  49. p1[i] = p2[i] = i;
  50. size[i] = 1;
  51. }
  52. start = clock();
  53. for(i = 0; i < m; i++){
  54. Union1(a[i], b[i]);
  55. }
  56. finish = clock();
  57. cout << finish - start << endl;
  58.  
  59. start = clock();
  60. for(i = 0; i < m; i++){
  61. Union2(a[i], b[i]);
  62. }
  63. finish = clock();
  64. cout << finish - start << endl;
  65.  
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement