f = map(int, input().split(' ')) n, q = next(f), next(f) people = [0] * n # 1 - ma kompa # 0 - nie ma kompa # -1 - nie wiadomo pairs = [] def addPair(a, b): pairs.append([a, b]) def getPair(a): for p in pairs: if(a == p[0]): return p[1] elif(a == p[1]): return p[0] return 'false' # lmao def removePair(a, b): for i in range(len(pairs)): p = pairs[i] if((a == p[0] and b == p[1]) or (a == p[1] and b == p[0])): del pairs[i] return True return False def resolvePair(a, toSet): p = getPair(a) if(p == 'false'): return removePair(a, p) people[p] = toSet # print(p, toSet) resolvePair(a, toSet) resolvePair(p, toSet) def confirmedPC(a): # a now has a pc # people[a] = 1 # set this manually while(True): b = getPair(a) if(not b): return people[b] = 0 #removePair(a, b) def removedPC(a): # people[a] = 0 # set this manually while(True): b = getPair(a) if(not b): return people[b] = 1 removePair(a, b) for _ in range(q): e = input().split(' ') if(e[0] == '+'): a, b = int(e[1]) - 1, int(e[2]) - 1 if(a == b): people[a] = 1 # confirmedPC(a) resolvePair(a, 0) continue if(people[a] == 1): people[b] = 1 resolvePair(b, 0) elif(people[b] == 1): people[a] = 1 resolvePair(a, 0) else: people[a] = -1 people[b] = -1 addPair(a, b) elif(e[0] == '-'): c = int(e[1]) - 1 people[c] = 0 resolvePair(c, 1) elif(e[0] == '?'): d = int(e[1]) - 1 p = people[d] print('?' if p == -1 else str(p), end='') # print(e, ' ', pairs)
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | f = map(int, input().split(' ')) n, q = next(f), next(f) people = [0] * n # 1 - ma kompa # 0 - nie ma kompa # -1 - nie wiadomo pairs = [] def addPair(a, b): pairs.append([a, b]) def getPair(a): for p in pairs: if(a == p[0]): return p[1] elif(a == p[1]): return p[0] return 'false' # lmao def removePair(a, b): for i in range(len(pairs)): p = pairs[i] if((a == p[0] and b == p[1]) or (a == p[1] and b == p[0])): del pairs[i] return True return False def resolvePair(a, toSet): p = getPair(a) if(p == 'false'): return removePair(a, p) people[p] = toSet # print(p, toSet) resolvePair(a, toSet) resolvePair(p, toSet) def confirmedPC(a): # a now has a pc # people[a] = 1 # set this manually while(True): b = getPair(a) if(not b): return people[b] = 0 #removePair(a, b) def removedPC(a): # people[a] = 0 # set this manually while(True): b = getPair(a) if(not b): return people[b] = 1 removePair(a, b) for _ in range(q): e = input().split(' ') if(e[0] == '+'): a, b = int(e[1]) - 1, int(e[2]) - 1 if(a == b): people[a] = 1 # confirmedPC(a) resolvePair(a, 0) continue if(people[a] == 1): people[b] = 1 resolvePair(b, 0) elif(people[b] == 1): people[a] = 1 resolvePair(a, 0) else: people[a] = -1 people[b] = -1 addPair(a, b) elif(e[0] == '-'): c = int(e[1]) - 1 people[c] = 0 resolvePair(c, 1) elif(e[0] == '?'): d = int(e[1]) - 1 p = people[d] print('?' if p == -1 else str(p), end='') # print(e, ' ', pairs) |