Advertisement
serega1112

Banket

Feb 18th, 2022
666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  1. from collections import defaultdict
  2.  
  3. n, m = map(int, input().split())
  4. g = defaultdict(list)
  5. for i in range(m):
  6.     a, b = map(int, input().split())
  7.     g[a].append(b)
  8.     g[b].append(a)
  9.  
  10.  
  11. visited = [0] * 101
  12.  
  13. def dfs(node, color):
  14.     if not visited[node]:
  15.         visited[node] = color
  16.         res = True
  17.         for neigh in g[node]:
  18.             res = res and dfs(neigh, -1 * color)
  19.         return res
  20.     elif visited[node] == color:
  21.         return True
  22.     else:
  23.         return False
  24.  
  25. res = True
  26. for v in range(1, n+1):
  27.     if not visited[v]:
  28.         res = res and dfs(v, 1)
  29.  
  30. if res:
  31.     print('YES')
  32.     print(' '.join([str(i) for i, v in enumerate(visited) if v == 1]))
  33. else:
  34.     print('NO')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement