[Go] Tarjan algorithm solution and its modification
Solution based on ranking\n\nfunc criticalConnections(n int, connections [][]int) [][]int {\n\tgraph := make([][]int, n)\n\tfor _, connection := range connectio
Solution based on ranking\n```\nfunc criticalConnections(n int, connections [][]int) [][]int {\n\tgraph := make([][]int, n)\n\tfor _, connection := range connections {\n\t\tgraph[connection[0]] = append(graph[connection[0]], connection[1])\n\t\tgraph[connection[1]] = append(graph[connection[1]], connection[0])\n\t}\n\n\tranks := make([]int, n)\n\tfor i := 0; i < n; i++ {\n\t\tranks[i] = -2\n\t}\n\tcyclic := make(map[[2]int]struct{})\n\tvar dfs func(node, depth int) int\n\n\tdfs = func(node, depth int) int {\n\t\tif ranks[node] >= 0 {\n\t\t\treturn ranks[node]\n\t\t}\n\t\tranks[node] = depth\n\t\tminBackDepth := n\n\n\t\tfor _, next := range graph[node] {\n\t\t\tif ranks[next] == depth-1 {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tbackDepth := dfs(next, depth+1)\n\t\t\tif backDepth <= depth {\n\t\t\t\tcyclic[[2]int{node, next}] = struct{}{}\n\t\t\t}\n\t\t\tminBackDepth = min(minBackDepth, backDepth)\n\t\t}\n\t\treturn minBackDepth\n\t}\n\n\tdfs(0, 0)\n\n\tresult := make([][]int, 0)\n\tfor _, connection := range connections {\n\t\tif _, ok := cyclic[[2]int{connection[0], connection[1]}]; ok {\n\t\t\tcontinue\n\t\t}\n\t\tif _, ok := cyclic[[2]int{connection[1], connection[0]}]; ok {\n\t\t\tcontinue\n\t\t}\n\t\tresult = append(result, connection)\n\t}\n\treturn result\n}\n\nfunc min(a, b int) int {\n\tif a < b {\n\t\treturn a\n\t}\n\treturn b\n}\n```\n\n\nTarjan algorithm\n```\nfunc criticalConnections(n int, connections [][]int) [][]int {\n\tgraph := make([][]int, n)\n\tfor _, connection := range connections {\n\t\tgraph[connection[0]] = append(graph[connection[0]], connection[1])\n\t\tgraph[connection[1]] = append(graph[connection[1]], connection[0])\n\t}\n\n\tvar dfs func(int, int)\n\tvisited := make(map[int]int)\n\tvar timer int\n\n\tcritical := make([][]int, 0)\n\tdfs = func(v int, p int) {\n\t\tvisited[v] = timer\n\t\tcurrentTime := timer\n\t\ttimer++\n\n\t\tfor i := 0; i < len(graph[v]); i++ {\n\t\t\tto := graph[v][i]\n\t\t\tif to == p {\n\t\t\t\tcontinue\n\t\t\t}\n\n\t\t\tif _, ok := visited[to]; !ok {\n\t\t\t\tdfs(to, v)\n\t\t\t}\n\t\t\tvisited[v] = min(visited[to], visited[v])\n\t\t\tif currentTime < visited[to] {\n\t\t\t\tcritical = append(critical, []int{v, to})\n\t\t\t}\n\t\t}\n\t}\n\tdfs(0, 0)\n\n\treturn critical\n}\n\nfunc min(a, b int) int {\n\tif a < b {\n\t\treturn a\n\t}\n\treturn b\n}\n```
[]