fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10.  
  11. public static int n = 7;
  12.  
  13. public static int[][] edges = {
  14. {0, 1},
  15. {1, 2},
  16. {2, 3},
  17. {4, 5},
  18. {5, 6}
  19. };
  20.  
  21. public static int[] parent = {0, 1, 2, 3, 4, 5, 6};
  22.  
  23. public static int find(int a){
  24. if(parent[a] == a){
  25. return a;
  26. }else{
  27. return find(parent[a]);
  28. }
  29. }
  30.  
  31. public static void union(int a, int b){
  32.  
  33. int root1 = find(a);
  34. int root2 = find(b);
  35.  
  36. if(root1 == root2){
  37.  
  38. }else{
  39. parent[root1] = root2;
  40. }
  41. }
  42. public static void main (String[] args) throws java.lang.Exception
  43. {
  44. // your code goes here
  45.  
  46. union(3, 5);
  47.  
  48. for(int i : parent){
  49. System.out.print(i + " ");
  50. }
  51.  
  52. }
  53. }
Success #stdin #stdout 0.09s 55584KB
stdin
Standard input is empty
stdout
0 1 2 5 4 5 6