Advertisement
1988coder

685. Redundant Connection II

Dec 30th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.31 KB | None | 0 0
  1. /*
  2. Using union-find with rank and path compression.
  3.  
  4. Refer: https://leetcode.com/problems/redundant-connection-ii/discuss/108045/C++Java-Union-Find-with-explanation-O(n)
  5.  
  6. Time Complexity: O(N) Space Complexity : O(N)
  7.  
  8. N = number of edges or the length of edges array.
  9. */
  10. class Solution {
  11.     class UnionFind {
  12.         int[] parents;
  13.         int[] ranks;
  14.  
  15.         UnionFind(int n) {
  16.             parents = new int[n + 1];
  17.             ranks = new int[n + 1];
  18.         }
  19.  
  20.         public int findRoot(int x) {
  21.             if (x != parents[x]) {
  22.                 parents[x] = findRoot(parents[x]);
  23.             }
  24.             return parents[x];
  25.         }
  26.  
  27.         public boolean union(int x, int y) {
  28.             int pX = findRoot(x);
  29.             int pY = findRoot(y);
  30.  
  31.             if (pX == pY) {
  32.                 return false;
  33.             }
  34.  
  35.             if (ranks[pX] >= ranks[pY]) {
  36.                 ranks[pX] = ranks[pX] == ranks[pY] ? ranks[pX] + 1 : ranks[pX];
  37.                 parents[pY] = pX;
  38.             } else {
  39.                 parents[pX] = pY;
  40.             }
  41.             return true;
  42.         }
  43.     }
  44.  
  45.     public int[] findRedundantDirectedConnection(int[][] edges) {
  46.         if (edges == null || edges.length == 0 || edges[0].length == 0) {
  47.             return new int[] { -1, -1 };
  48.         }
  49.  
  50.         int[] cand1 = new int[] { -1, -1 };
  51.         int[] cand2 = new int[] { -1, -1 };
  52.         UnionFind dsu = new UnionFind(edges.length);
  53.  
  54.         for (int[] e : edges) {
  55.             int src = e[0];
  56.             int dst = e[1];
  57.             if (dsu.parents[dst] != 0) {
  58.                 cand1 = new int[] { dsu.parents[dst], dst };
  59.                 cand2 = new int[] { src, dst };
  60.                 e[1] = -1;
  61.             } else {
  62.                 dsu.parents[dst] = src;
  63.             }
  64.         }
  65.  
  66.         // Initializing the parents array for Union-Find
  67.         for (int i = 1; i <= edges.length; i++) {
  68.             dsu.parents[i] = i;
  69.         }
  70.  
  71.         for (int[] e : edges) {
  72.             if (e[1] == -1) {
  73.                 continue;
  74.             }
  75.             if (!dsu.union(e[0], e[1])) {
  76.                 if (cand1[0] == -1) {
  77.                     return e;
  78.                 } else {
  79.                     return cand1;
  80.                 }
  81.             }
  82.         }
  83.  
  84.         return cand2;
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement