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
[n, N] = input().split(' ')

computers = []
users = [0] * (int(n)+1)
answer = ''

def has_other_neighbors(vertex):
    for tup in computers:
        if vertex in tup:
            neighbor = tup[1] if tup[0] == vertex else tup[0]
            if neighbor != vertex:
                return True
    return False

def find_cycles(target_vertices):
    cycles = []

    def dfs(vertex, path, visited, parent):
        visited.add(vertex)
        for tup in computers:
            if vertex in tup:
                neighbor = tup[1] if tup[0] == vertex else tup[0]
                if neighbor not in visited:
                    if dfs(neighbor, path + [vertex], visited.copy(), vertex):
                        return True
                elif neighbor != parent and neighbor in target_vertices:
                    cycles.append(path + [vertex, neighbor])
                    return True
        return False

    for vertex in target_vertices:
        dfs(vertex, [], set(), None)
    
    if cycles:
        for cycle in cycles:
            for elem in cycle:
                users[int(elem)] = 1
    else:
        users[int(target_vertices[0])] = '?'
        users[int(target_vertices[1])] = '?'
        
def remove_comp(user_id):
    users[user_id] = '0'
    if not has_other_neighbors(user_id):
        users[user_id] = '0'

for event in range(0,int(N)):
    event_arr = input().split(' ')
    operator = event_arr[0]
    id_1 = int(event_arr[1])
    if len(event_arr) == 3:
        id_2 = int(event_arr[2])

    if operator == '?':
        answer += str(users[id_1])
    elif operator == '+':
        if ((id_1, id_2) in computers) or ((id_2, id_1) in computers):
            users[id_1] = '1'
            users[id_2] = '1'
        else:
            if id_1 == id_2:
                users[id_1] = '1'
            computers.append((id_1, id_2))
            find_cycles((id_1, id_2))
    elif operator == '-':
        remove_comp(id_1)

print(answer)