python.graphs

By Vitaly Pavlenko, ago, written in Python.
URL http://pastecode.org/index.php/view/65614727
Download Paste or View RawExpand paste to full width of browser | Change Viewing Options
  1. black, grey, white = range(3)
  2.  
  3. def dfs(colors, adj_matrix, v):
  4.     colors[v] = grey
  5.     for w, edge in enumerate(adj_matrix[v]):
  6.         if (edge == 1) and (colors[w] == white):
  7.             dfs(colors, adj_matrix, w)
  8.     colors[v] = black
  9.  
  10. n, s = map(int, input().split())
  11. s -= 1
  12. adj_matrix = [map(int, input().split()) for j in range(n)]
  13.  
  14. colors = [white for i in range(n)]
  15.  
  16. dfs(colors, adj_matrix, s)
  17.  
  18. print(colors.count(black))

Here you can reply to the paste above

Make Private

   

Feeling clever? Set some advanced options.