import sys DEBUG = True DEBUG = False nq = [int(i) for i in input().split(' ') if i != ''] n = nq[0] q = nq[1] m = [0 for _ in range(n + 1)] p = [[] for _ in range(n + 1)] for i in range(q): line = input() DEBUG and print(line) line = line.split(' ') if line[0] == '?': an = m[int(line[1])] print(an if an != -1 else '?', end='\n' if DEBUG else '') elif line[0] == '-': m[int(line[1])] = 0 for _p in p[int(line[1])]: DEBUG and print('remove for ', line[1]) try: p[_p].remove(int(line[1])) except ValueError: pass # if len(p[_p]) == 0: m[_p] = 0 p[_p] = [] p[int(line[1])] = [] elif line[0] == '+': if int(line[1]) == int(line[2]): m[int(line[1])] = 1 else: if m[int(line[1])] == 1: m[int(line[2])] = 1 if len(p[int(line[2])]) > 0: mx = p[int(line[2])][0] m[mx] = 1 del p[int(line[2])][0] p[mx].remove(int(line[2])) elif m[int(line[2])] == 1: m[int(line[1])] = 1 if len(p[int(line[1])]) > 0: mx = p[int(line[1])][0] m[mx] = 1 del p[int(line[1])][0] p[mx].remove(int(line[1])) else: if int(line[2]) in p[int(line[1])] and int(line[1]) in p[int(line[2])]: m[int(line[1])] = 1 m[int(line[2])] = 1 p[int(line[1])].remove(int(line[2])) p[int(line[2])].remove(int(line[1])) else: m[int(line[1])] = -1 m[int(line[2])] = -1 p[int(line[1])].append(int(line[2])) p[int(line[2])].append(int(line[1])) DEBUG and print(m) DEBUG and print(p) print('')
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 62 63 | import sys DEBUG = True DEBUG = False nq = [int(i) for i in input().split(' ') if i != ''] n = nq[0] q = nq[1] m = [0 for _ in range(n + 1)] p = [[] for _ in range(n + 1)] for i in range(q): line = input() DEBUG and print(line) line = line.split(' ') if line[0] == '?': an = m[int(line[1])] print(an if an != -1 else '?', end='\n' if DEBUG else '') elif line[0] == '-': m[int(line[1])] = 0 for _p in p[int(line[1])]: DEBUG and print('remove for ', line[1]) try: p[_p].remove(int(line[1])) except ValueError: pass # if len(p[_p]) == 0: m[_p] = 0 p[_p] = [] p[int(line[1])] = [] elif line[0] == '+': if int(line[1]) == int(line[2]): m[int(line[1])] = 1 else: if m[int(line[1])] == 1: m[int(line[2])] = 1 if len(p[int(line[2])]) > 0: mx = p[int(line[2])][0] m[mx] = 1 del p[int(line[2])][0] p[mx].remove(int(line[2])) elif m[int(line[2])] == 1: m[int(line[1])] = 1 if len(p[int(line[1])]) > 0: mx = p[int(line[1])][0] m[mx] = 1 del p[int(line[1])][0] p[mx].remove(int(line[1])) else: if int(line[2]) in p[int(line[1])] and int(line[1]) in p[int(line[2])]: m[int(line[1])] = 1 m[int(line[2])] = 1 p[int(line[1])].remove(int(line[2])) p[int(line[2])].remove(int(line[1])) else: m[int(line[1])] = -1 m[int(line[2])] = -1 p[int(line[1])].append(int(line[2])) p[int(line[2])].append(int(line[1])) DEBUG and print(m) DEBUG and print(p) print('') |