def modernization_bajtocji():
n, q = map(int, input().split())
has_computer = set()
might_have_computer = {}
result = []
for _ in range(q):
event = input().split()
event_type = event[0]
if event_type == '+':
a, b = map(int, event[1:])
if a == b or a in has_computer or b in has_computer:
has_computer.add(a if a == b or b in has_computer else b)
else:
might_have_computer.setdefault(a, set()).add(b)
might_have_computer.setdefault(b, set()).add(a)
elif event_type == '-':
c = int(event[1])
has_computer.discard(c)
for other in list(might_have_computer.keys()):
might_have_computer[other].discard(c)
if len(might_have_computer[other]) == 1:
single_candidate = might_have_computer[other].pop()
has_computer.add(single_candidate)
del might_have_computer[other]
elif event_type == '?':
d = int(event[1])
if d in has_computer:
result.append('1')
elif d in might_have_computer and len(might_have_computer[d]) == 1:
result.append('?')
else:
result.append('0')
return ''.join(result)
print(modernization_bajtocji())
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 | def modernization_bajtocji(): n, q = map(int, input().split()) has_computer = set() might_have_computer = {} result = [] for _ in range(q): event = input().split() event_type = event[0] if event_type == '+': a, b = map(int, event[1:]) if a == b or a in has_computer or b in has_computer: has_computer.add(a if a == b or b in has_computer else b) else: might_have_computer.setdefault(a, set()).add(b) might_have_computer.setdefault(b, set()).add(a) elif event_type == '-': c = int(event[1]) has_computer.discard(c) for other in list(might_have_computer.keys()): might_have_computer[other].discard(c) if len(might_have_computer[other]) == 1: single_candidate = might_have_computer[other].pop() has_computer.add(single_candidate) del might_have_computer[other] elif event_type == '?': d = int(event[1]) if d in has_computer: result.append('1') elif d in might_have_computer and len(might_have_computer[d]) == 1: result.append('?') else: result.append('0') return ''.join(result) print(modernization_bajtocji()) |
English