My DFS and BSF solutions
share my DFS and BFS solution, although there are not effcient\n\npublic int[] bfs(List<Set<Integer>> adj, int[][]edges){\n     \n        for(int[] e: edges){\n
share my DFS and BFS solution, although there are not effcient\n```\npublic int[] bfs(List<Set<Integer>> adj, int[][]edges){\n     \n        for(int[] e: edges){\n            int from = e[0], to = e[1];\n            \n            Queue<Integer> q = new ArrayDeque<>();\n            Set<Integer> visited = new HashSet<>();\n            q.add(from);\n            visited.add(from);\n            while( !q.isEmpty() ){\n                \n                int p = q.poll();\n                if(adj.get(p).contains(to))\n                    return e;\n                \n                for(int w: adj.get(p)){\n                    if(visited.contains(w))   // avoid visit the last adj node\n                        continue;\n                    q.add(w);\n                    visited.add(w);\n                }\n            }\n            \n            adj.get(from).add(to);\n            adj.get(to).add(from);\n        }\n        return new int[]{-1,-1};\n    }\n\n\n public int[] dfs(List<Set<Integer>> adj, int[][]edges){\n        for(int[] e: edges){\n            int from = e[0], to = e[1];\n            if(dfsR(adj, edges, from, to, from)) // \u6709\u73af\n                return e;\n            adj.get(from).add(to);\n            adj.get(to).add(from);\n        }\n        return new int[]{-1,-1};\n    }\n```\n```    \n// return true if has cycle\n    public boolean dfsR(List<Set<Integer>> adj, int[][]edges, int cur, int target, int pre){\n        \n        if(adj.get(cur).contains(target)) \n            return true;\n        \n        for(int w: adj.get(cur)){\n            if(w == pre)    // skip the last adj node\n                continue;\n            if( dfsR(adj,edges,w,target,cur) )\n                return true;\n        }\n        \n        return false;\n    }\n```
[]