Advertisement
always2hin

Untitled

Jan 12th, 2020
630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.42 KB | None | 0 0
  1. func criticalConnections(n int, connections [][]int) [][]int {
  2.     graph := make([][]int, n)
  3.     time := make([]int, n)
  4.     parTime := make([]int, n)
  5.     par := make([]int, n)
  6.     ret := make([][]int, 0)
  7.    
  8.     for i := range graph {
  9.         graph[i] = make([]int, 0)
  10.         time[i] = -1
  11.         parTime[i] = -1
  12.     }
  13.    
  14.     for i := range connections {
  15.         graph[connections[i][0]] = append(graph[connections[i][0]], connections[i][1])        
  16.         graph[connections[i][1]] = append(graph[connections[i][1]], connections[i][0])
  17.     }
  18.    
  19.     dfs(graph, time, parTime, par, 0, &ret)
  20.    
  21.     return ret
  22. }
  23.  
  24. var t int = 0
  25.  
  26. func dfs(graph [][]int, time[]int, parTime[]int, par[]int, src int, ret *[][]int) {
  27.     t++
  28.     time[src] = t
  29.     parTime[src] = t
  30.    
  31.     for i := range graph[src] {
  32.         if time[graph[src][i]] == -1 {
  33.             par[graph[src][i]] = src
  34.             dfs(graph, time, parTime, par, graph[src][i], ret)
  35.             if parTime[graph[src][i]] > time[src] {
  36.                 *ret = append(*ret, []int{src, graph[src][i]})
  37.             } else {
  38.                 if parTime[src] > parTime[graph[src][i]] {
  39.                     parTime[src] = parTime[graph[src][i]]
  40.                 }
  41.             }
  42.         } else if par[src] != graph[src][i] {
  43.             if parTime[src] > time[graph[src][i]] {
  44.                 parTime[src] = time[graph[src][i]]
  45.             }
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement