fork download
  1. // GROUP: 3
  2. // ID: 20150181
  3. // Assign: 02
  4. // Assign: DisjointSets
  5. // UVA: 10608
  6. // Name: Amr Tarek Mahmoud
  7. // UVA Handle: Amr_Tarek
  8.  
  9. #include <cstdio>
  10. #include <cstdlib>
  11. #include <cstring>
  12. #include <iostream>
  13. using namespace std;
  14.  
  15. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16.  
  17. struct DisjointSets
  18. {
  19. int n;
  20. int* parent;
  21. int* num_nodes;
  22.  
  23. void Initialize(int _n){
  24. n = _n;
  25. parent = new int [n];
  26. num_nodes = new int [n];
  27. for(int i=0 ; i<n ; i++){
  28. parent[i] = i;
  29. num_nodes[i] = 1;
  30. }
  31. }
  32. void Destroy(){
  33. delete []parent;
  34. delete []num_nodes;
  35. }
  36. int Find(int i){
  37. if(parent[i] == i)return i;
  38. return parent[i] = Find(parent[i]);
  39. }
  40. bool Union(int i, int j){
  41. int a = Find(i) , b = Find(j);
  42. if(a == b)return false;
  43. else{
  44. if(num_nodes[a] > num_nodes[b])
  45. parent[b] = a;
  46. else parent[a] = b;
  47. }
  48. return true;
  49. }
  50. };
  51.  
  52. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  53.  
  54. int main()
  55. {
  56. DisjointSets tree;
  57. tree.Initialize(10);
  58. for(int i=0 ; i<10 ; i++){
  59. cout<<parent[i]<<endl;
  60. }
  61. cout<<endl;
  62. tree.Union(3,7);
  63. cout<<parent[3]<<' '<<parent[7]<<endl;
  64.  
  65. return 0;
  66. }
  67.  
  68. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  69.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:59:9: error: ‘parent’ was not declared in this scope
   cout<<parent[i]<<endl;
         ^~~~~~
prog.cpp:63:8: error: ‘parent’ was not declared in this scope
  cout<<parent[3]<<' '<<parent[7]<<endl;
        ^~~~~~
stdout
Standard output is empty