package leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * https://leetcode.com/problems/redundant-connection/
 */
public class Problem684 {
    public int[] findRedundantConnection(int[][] edges) {
        // TODO
        return new int[0];
    }

    private static class UnionFind {
        private final Map<Integer, Integer> map = new HashMap<>();

        public void union(int a, int b) {
            if (!map.containsKey(a)) {
                map.put(a, a);
            }
            if (!map.containsKey(b)) {
                map.put(b, b);
            }
            while (!connected(a, b)) {
                int value = map.get(a);
                for (Map.Entry<Integer, Integer> e : map.entrySet()) {
                    if (e.getValue().equals(value)) {
                        map.put(e.getKey(), map.get(b));
                    }
                }
            }
        }

        public boolean connected(int a, int b) {
            return map.get(a).equals(map.get(b));
        }
    }

    public static void main(String[] args) {
        Problem684 prob = new Problem684();
        System.out.println(prob.findRedundantConnection(new int[][]{
            {1, 2}, {1, 3}, {2, 3}
        })); // [2,3]
        System.out.println(prob.findRedundantConnection(new int[][]{
            {1,2}, {2,3}, {3,4}, {1,4}, {1,5}
        })); // [1,4]
    }
}

@@ -589,13 +589,10 @@
     
-while
+if
  (!c
@@ -605,32 +605,66 @@
 ed(a, b)) %7B%0A    
+            return;%0A            %7D%0A
             int 
@@ -679,28 +679,24 @@
 map.get(a);%0A
-    
             
@@ -762,28 +762,24 @@
             
-   
- 
 if (e.getVal
@@ -812,36 +812,32 @@
                 
-    
 map.put(e.getKey
@@ -857,30 +857,8 @@
 ));%0A
-                    %7D%0A
     

Update problem 684