withopen("B-large-practice.in") as f: C = int(f.readline()) for i inrange(C): N = int(f.readline()) M = int(f.readline()) l = [] for j inrange(M): tmp = {} line = f.readline().strip().split() for k inrange(int(line[0])): tmp[int(line[2 * k + 1])] = int(line[2 * k + 2]) l.append(tmp) ans = {} # solution logic insert below possible = True whilelen(l) and possible: new_malted = None for c in l: iflen(c) > 1: continue if c[c.keys()[0]] == 1: ans[c.keys()[0]] = 1 new_malted = c.keys()[0] break elif c.keys()[0] in ans: possible = False break if new_malted isNone: break
del_list = [] for c in l: if new_malted in c: if c[new_malted]: del_list.append(c) continue else: del c[new_malted] iflen(c) == 0: possible = False break for c in del_list: l.remove(c)
# output result res = "Case #{0}:".format(i + 1) ifnot possible: res += " IMPOSSIBLE" else: for i inrange(N): if i + 1in ans: res += " 1" else: res += " 0" print res