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
temp = tuple(input().split())

liczbaMieszkancow = int(temp[0])
liczbaAkcji = int(temp[1])

komputery = []

result = ""

for i in range(liczbaMieszkancow):
    komputery.append(set())

for i in range(liczbaAkcji):
    temp = list(input().split())
    temp[1] = int(temp[1])
    if temp[0] == "?":
        if len(komputery[temp[1]]) == 0:
            result += "0"
        elif temp[1] in komputery[temp[1]]:
            result += "1"
        else:
            result += "?"

    elif temp[0] == "+":

        temp[2] = int(temp[2])

        if temp[1] in komputery[temp[1]]:
            komputery[temp[2]] = {temp[2]}
        elif temp[2] in komputery[temp[2]]:
            komputery[temp[1]] = {temp[1]}
        elif temp[2] in komputery[temp[1]]:
            komputery[temp[1]] = {temp[1]}
            komputery[temp[2]] = {temp[2]}
        else:
            komputery[temp[1]].add(temp[2])
            komputery[temp[2]].add(temp[1])

    elif temp[0] == "-":
        for i in komputery[temp[1]]:
            komputery[i].update(komputery[temp[1]])
            komputery[i].remove(temp[1])
        komputery[temp[1]] = set()
print(result)