Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. def dfs(v):
  2. for i in g[v]:
  3. if not used[i]:
  4. used[i] = 1
  5. if not colors[v]: # red
  6. colors[i] = 1
  7. else:
  8. colors[i] = 0
  9. dfs(i)
  10.  
  11. else:
  12. if colors[v] == colors[i]:
  13. print(-1)
  14. exit(0)
  15.  
  16. y = 0
  17. n = int(input())
  18.  
  19. g = dict()
  20.  
  21. for i in range(1, n + 1):
  22.  
  23. if i not in g:
  24. g[i] = set()
  25. m = [int(i) for i in input().split()]
  26.  
  27. for j in m:
  28. if j == 0:
  29. break
  30. g[i].add(j)
  31. if j not in g:
  32. g[j] = set()
  33.  
  34. g[j].add(i)
  35.  
  36. used = [0] * (n + 1)
  37. colors = [0] * (n + 1)
  38.  
  39. dfs(1)
  40. for i in range(1, n+1):
  41. print(colors[i], end="")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement