line1 = input() line1 = list(map(int, line1.split(" "))) n = line1[0] n_lines = line1[1] adj = [set() for _ in range(n)] states = [0] * n ans = [] seen = set() def dfs(n): if n in seen: return states[n] = 2 for nn in adj[n]: if states[nn] == 1: dfs(nn) for _ in range(n_lines): ll = input() ll = ll.split(" ") op = ll[0] if op in ["?", "-"]: n1 = int(ll[1]) - 1 if op == "?": if states[n1] == 1: ans.append("?") elif states[n1] == 0: ans.append("0") else: ans.append("1") else: states[n1] = 0 else: n1 = int(ll[1]) - 1 n2 = int(ll[2]) - 1 if n1 == n2: states[n1] = 2 continue if states[n1] == 1 and states[n2] == 1: adj[n1].add(n2) adj[n2].add(n1) seen = set() dfs(n1) elif states[n1] in [0, 1] and states[n2] in [0, 1]: adj[n1].add(n2) adj[n2].add(n1) states[n1] = 1 states[n2] = 1 else: states[n1] = 2 states[n2] = 2 print("".join(ans))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | line1 = input() line1 = list(map(int, line1.split(" "))) n = line1[0] n_lines = line1[1] adj = [set() for _ in range(n)] states = [0] * n ans = [] seen = set() def dfs(n): if n in seen: return states[n] = 2 for nn in adj[n]: if states[nn] == 1: dfs(nn) for _ in range(n_lines): ll = input() ll = ll.split(" ") op = ll[0] if op in ["?", "-"]: n1 = int(ll[1]) - 1 if op == "?": if states[n1] == 1: ans.append("?") elif states[n1] == 0: ans.append("0") else: ans.append("1") else: states[n1] = 0 else: n1 = int(ll[1]) - 1 n2 = int(ll[2]) - 1 if n1 == n2: states[n1] = 2 continue if states[n1] == 1 and states[n2] == 1: adj[n1].add(n2) adj[n2].add(n1) seen = set() dfs(n1) elif states[n1] in [0, 1] and states[n2] in [0, 1]: adj[n1].add(n2) adj[n2].add(n1) states[n1] = 1 states[n2] = 1 else: states[n1] = 2 states[n2] = 2 print("".join(ans)) |