python.graphs

Go BackChange Paste Viewing Options
black, grey, white = range(3)
 
def dfs(colors, adj_matrix, v):
    colors[v] = grey
    for w, edge in enumerate(adj_matrix[v]):
        if (edge == 1) and (colors[w] == white):
            dfs(colors, adj_matrix, w)
    colors[v] = black
 
n, s = map(int, input().split())
s -= 1
adj_matrix = [map(int, input().split()) for j in range(n)]
 
colors = [white for i in range(n)]
 
dfs(colors, adj_matrix, s)
 
print(colors.count(black))			
Go Back