Java Union Find + DFS + BFS
DFS\n~~~java\n\n    public int[] findRedundantConnection(int[][] edges) {\n        int m = edges.length;\n        Map> map = new HashMap<>();\n        for (int 
* DFS\n~~~java\n\n    public int[] findRedundantConnection(int[][] edges) {\n        int m = edges.length;\n        Map<Integer, Set<Integer>> map = new HashMap<>();\n        for (int i = 1; i <= m; i++) {\n            map.put(i, new HashSet<>());\n        }\n        \n        for (int[] edge : edges) {\n            if (dfs(new HashSet<>(), map, edge[0], edge[1])) return edge;\n            map.get(edge[0]).add(edge[1]);\n            map.get(edge[1]).add(edge[0]);\n        }\n        \n        return new int[]{-1,-1};\n    }\n    \n    private boolean dfs(Set<Integer> visited, Map<Integer, Set<Integer>> map,\n                       int src, int target) {\n        if (src == target) return true;\n        visited.add(src);\n        for (int next : map.get(src)) {\n            if (!visited.contains(next)) {\n                if (dfs(visited, map, next, target)) return true;\n            }\n        }\n        return false;\n    }\n\n~~~\n\n* BFS\n\n~~~java\n\n    public int[] findRedundantConnection(int[][] edges) {\n        int m = edges.length;\n        Map<Integer, Set<Integer>> map = new HashMap<>();\n        for (int i = 1; i <= m; i++) {\n            map.put(i, new HashSet<>());\n        }\n        \n        for (int[] edge : edges) {\n            if (bfs(map, edge[0], edge[1])) return edge;\n            map.get(edge[0]).add(edge[1]);\n            map.get(edge[1]).add(edge[0]);\n        }\n        \n        return new int[]{-1,-1};\n    }\n    \n    private boolean bfs(Map<Integer, Set<Integer>> map,\n                       int src, int target) {\n        Queue<Integer> q = new LinkedList<>();\n        q.offer(src);\n        Set<Integer> visited = new HashSet<>();\n        visited.add(src);\n        \n        while (!q.isEmpty()) {\n            int cur = q.poll();\n            if (cur == target) return true;\n            for (int next : map.get(cur)) {\n                if (visited.add(next)) {\n                    q.offer(next);\n                }\n            }\n        }\n        \n        return false;\n    }\n\n~~~\n\n* Union Find\n\n~~~java\n\n    int[] parent;\n    \n    public int[] findRedundantConnection(int[][] edges) {\n        int m = edges.length;\n        parent = new int[m + 1];\n        for (int i = 0; i < m; i++) parent[i] = i;\n        \n        for (int[] edge : edges) {\n            if (!union(edge[0], edge[1])) return edge;\n        }\n        \n        return new int[]{-1,-1};\n    }\n    \n    private boolean union(int x, int y) {\n        int p1 = find(x), p2 = find(y);\n        if (p1 == p2) return false;\n        parent[p1] = p2;\n        return true;\n    }\n    \n    private int find(int x) {\n        while (x != parent[x]) {\n            x = parent[x];\n            parent[x] = parent[parent[x]];\n        }\n        return x;\n    }\n\n\n~~~
[]