Advertisement
nq1s788

dfs

Apr 9th, 2024
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.31 KB | None | 0 0
  1. def dfs(h): #h->v
  2.     used[h] = True
  3.     for v in g[h]:
  4.         if not used[v]:
  5.             dfs(v)
  6.  
  7.  
  8. n, m = map(int, input().split())
  9. g = [[] for i in range(n)]
  10. for i in range(m):
  11.     x, y = map(int, input().split())
  12.     x -= 1
  13.     y -= 1
  14.     g[x].append(y)
  15.     g[y].append(x)
  16. used = [False] * n
  17. dfs(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement