Advertisement
thewitchking

Untitled

Dec 14th, 2023
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 KB | None | 0 0
  1. // Java program for the above approach
  2. import java.util.*;
  3. // Implementation of DSU
  4. class UnionFind {
  5.     private int[] parent;
  6.     private int[] rank;
  7.     private int time;
  8.     private int count;
  9.    
  10.     public UnionFind(int N)
  11.     {
  12.         parent = new int[N];
  13.         rank = new int[N];
  14.         time = 0;
  15.         count = N;
  16.        
  17.         for (int i = 0; i < N; i++) {
  18.             parent[i] = i;
  19.             rank[i] = 1;
  20.         }
  21.     }
  22.  
  23.     public int find(int node)
  24.     {
  25.         if (node == parent[node])
  26.             return node;
  27.         return parent[node] = find(parent[node]);
  28.     }
  29.    
  30.     public void performUnion(int u, int v, int updatedTime)
  31.     {
  32.         if (count == 1)
  33.             return;
  34.        
  35.         int rootX = find(u);
  36.         int rootY = find(v);
  37.        
  38.         if (rootX != rootY) {
  39.             if (rank[rootX] < rank[rootY]) {
  40.                 parent[rootX] = rootY;
  41.             }
  42.             else if (rank[rootX] > rank[rootY]) {
  43.                 parent[rootY] = rootX;
  44.             }
  45.             else {
  46.                 parent[rootX] = rootY;
  47.                 rank[rootY] += 1;
  48.             }
  49.            
  50.             time = updatedTime;
  51.             count--;
  52.         }
  53.     }
  54.    
  55.     public int getCount() { return count; }
  56.  
  57.     public int getTime() { return time; }
  58. }
  59.  
  60. class Main {
  61.     private static int earliestTime(List<int[]> arr, int N)
  62.     {
  63.         arr.sort(Comparator.comparingInt(a -> a[2]));
  64.  
  65.         UnionFind unionFind = new UnionFind(N);
  66.  
  67.         for (int[] it : arr) {
  68.             unionFind.performUnion(it[0], it[1], it[2]);
  69.         }
  70.  
  71.         return unionFind.getCount() == 1
  72.             ? unionFind.getTime()
  73.             : -1;
  74.     }
  75.  
  76.     public static void main(String[] args)
  77.     {
  78.         int N = 6;
  79.         List<int[]> arr
  80.             = Arrays.asList(new int[][] { { 0, 1, 4 },
  81.                                         { 3, 4, 5 },
  82.                                         { 2, 3, 14 },
  83.                                         { 1, 5, 24 },
  84.                                         { 2, 4, 12 },
  85.                                         { 0, 3, 42 },
  86.                                         { 1, 2, 41 },
  87.                                         { 4, 5, 11 } });
  88.  
  89.         System.out.println(earliestTime(arr, N));
  90.     }
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement