niyaznigmatullin

Untitled

Jan 29th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.47 KB | None | 0 0
  1. ans = []
  2. tests = int(input())
  3. for curt in range(tests):
  4.   input()
  5.   n, m = map(int, input().split())
  6.   p = [i for i in range(n)]
  7.   ok = True
  8.   for i in range(m):
  9.     v, u = map(int, input().split())
  10.     v -= 1
  11.     u -= 1
  12.     c1 = 0
  13.     c2 = 0
  14.     while p[v] != v:
  15.       v = p[v]
  16.       c1 = 1 - c1
  17.     while p[u] != u:
  18.       u = p[u]
  19.       c2 = 1 - c2
  20.     if v == u and c1 == c2:
  21.       ok = False
  22.     p[v] = u
  23.   ans.append(1 if ok else -1)
  24. print(*ans)
Advertisement
Add Comment
Please, Sign In to add comment