wrequiems

Untitled

Mar 9th, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.69 KB | None | 0 0
  1. n, m = map(int, input().split(' '))
  2.  
  3. graph = {}
  4. pairs = []
  5. for _ in range(m):
  6.     x, y = map(int, input().split(' '))
  7.     pairs.append((x, y))
  8.     graph.setdefault(x, [])
  9.     graph.setdefault(y, [])
  10.     graph[x].append(y)
  11.     graph[y].append(x)
  12.  
  13. subtree = [1] * (n + 1)
  14.  
  15. visited = [False] * (n + 1)
  16.  
  17. def dfs(node):
  18.     if not visited[node]:
  19.         visited[node] = True
  20.         for out in graph[node]:
  21.             if not visited[out]:
  22.                 dfs(out)
  23.                 subtree[node] += subtree[out]
  24.  
  25. dfs(1)
  26.  
  27. for x, y in pairs:
  28.     small_subtree = min(subtree[x], subtree[y])
  29.     print("Crossing edge {}-{} there are {} paths".format(x, y, small_subtree * (n - small_subtree)))
Add Comment
Please, Sign In to add comment