response = '' start_data = input().split(" ") n = int(start_data[0]) q = int(start_data[1]) graph = {} for x in range(1,n+1): graph[x] = [] for x in range (0,q): command = input().split(" ") if command[0] == '?': if len(graph[int(command[1])]) == 0: response+='0' elif int(command[1]) in graph[int(command[1])]: response+='1' else: response +='?' elif command[0] == '+': if int(command[1]) in graph[int(command[2])]: graph[int(command[1])].remove(int(command[2])) graph[int(command[2])].remove(int(command[1])) graph[int(command[1])].append(int(command[1])) graph[int(command[2])].append(int(command[2])) else: graph[int(command[1])].append(int(command[2])) graph[int(command[2])].append(int(command[1])) else: if len(graph[int(command[1])]) > 0: to_be_deleted = [] for x in graph[int(command[1])]: to_be_deleted.append(x) for x in range(0,len(to_be_deleted)): graph[int(command[1])].remove(to_be_deleted[x]) graph[to_be_deleted[x]].remove(int(command[1])) graph[to_be_deleted[x]].append(to_be_deleted[x]) print(response)
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 | response = '' start_data = input().split(" ") n = int(start_data[0]) q = int(start_data[1]) graph = {} for x in range(1,n+1): graph[x] = [] for x in range (0,q): command = input().split(" ") if command[0] == '?': if len(graph[int(command[1])]) == 0: response+='0' elif int(command[1]) in graph[int(command[1])]: response+='1' else: response +='?' elif command[0] == '+': if int(command[1]) in graph[int(command[2])]: graph[int(command[1])].remove(int(command[2])) graph[int(command[2])].remove(int(command[1])) graph[int(command[1])].append(int(command[1])) graph[int(command[2])].append(int(command[2])) else: graph[int(command[1])].append(int(command[2])) graph[int(command[2])].append(int(command[1])) else: if len(graph[int(command[1])]) > 0: to_be_deleted = [] for x in graph[int(command[1])]: to_be_deleted.append(x) for x in range(0,len(to_be_deleted)): graph[int(command[1])].remove(to_be_deleted[x]) graph[to_be_deleted[x]].remove(int(command[1])) graph[to_be_deleted[x]].append(to_be_deleted[x]) print(response) |