Advertisement
nq1s788

dfs проверка на двудольность

Apr 9th, 2024
704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.51 KB | None | 0 0
  1. def dfs(h):
  2.     global answ
  3.     used[h] = 1
  4.     for e in g[h]:
  5.         if used[e] == 0:
  6.             col[e] = col[h] ^ 1
  7.             dfs(e)
  8.         else:
  9.             if col[e] == col[h]:
  10.                 answ = False
  11.  
  12.  
  13. n, m = map(int, input().split())
  14. g = [[] for i in range(n)]
  15. for i in range(m):
  16.     x, y = map(int, input().split())
  17.     x -= 1
  18.     y -= 1
  19.     g[x].append(y)
  20.     g[y].append(x)
  21. used = [0] * n
  22. col = [-1] * n
  23. col[0] = 0
  24. answ = True #граф двудольный
  25. dfs(0)
  26. print(answ)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement