import sys def connect(a, b): moves.append(" ".join(["+", str(a), str(b)])) edges[a].add(b) edges[b].add(a) def disconnect(a, b): moves.append(" ".join(["-", str(a), str(b)])) edges[a].remove(b) edges[b].remove(a) moves = [] n = int(sys.stdin.readline().rstrip()) edges = {i: set() for i in range(1, n + 1)} target = {i: set() for i in range(1, n + 1)} ms = int(sys.stdin.readline().rstrip()) for i in range(ms): a, b = (int(i) for i in sys.stdin.readline().split()) edges[a].add(b) edges[b].add(a) md = int(sys.stdin.readline().rstrip()) for i in range(md): a, b = (int(i) for i in sys.stdin.readline().split()) target[a].add(b) target[b].add(a) visited = [False] * (n + 1) center = 1 for i in range(2, n + 1): if len(edges[i]) == 1: center = i break visited[center] = True queue = list(edges[center]) while queue: v = queue.pop(0) visited[v] = True if v not in edges[center]: connect(center, v) for u in edges[v]: if not visited[u]: queue.append(u) for i in range(1, center): redundant = edges[i] - target[i] - {center} lacking = target[i] - edges[i] for l in lacking: connect(i, l) for r in redundant: disconnect(i, r) for i in range(center+1, n+1): redundant = edges[i] - target[i] - {center} lacking = target[i] - edges[i] for l in lacking: connect(i, l) for r in redundant: disconnect(i, r) visited = [False] * (n + 1) start = 1 for i in range(2, n + 1): if len(target[i]) == 1: start = i break visited[start] = True queue = list(target[start]) while queue: v = queue.pop(0) visited[v] = True if v not in target[center] and v in edges[center]: disconnect(center, v) for u in target[v]: if not visited[u]: queue.append(u) print(len(moves)) for m in moves: print(m)
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 | import sys def connect(a, b): moves.append(" ".join(["+", str(a), str(b)])) edges[a].add(b) edges[b].add(a) def disconnect(a, b): moves.append(" ".join(["-", str(a), str(b)])) edges[a].remove(b) edges[b].remove(a) moves = [] n = int(sys.stdin.readline().rstrip()) edges = {i: set() for i in range(1, n + 1)} target = {i: set() for i in range(1, n + 1)} ms = int(sys.stdin.readline().rstrip()) for i in range(ms): a, b = (int(i) for i in sys.stdin.readline().split()) edges[a].add(b) edges[b].add(a) md = int(sys.stdin.readline().rstrip()) for i in range(md): a, b = (int(i) for i in sys.stdin.readline().split()) target[a].add(b) target[b].add(a) visited = [False] * (n + 1) center = 1 for i in range(2, n + 1): if len(edges[i]) == 1: center = i break visited[center] = True queue = list(edges[center]) while queue: v = queue.pop(0) visited[v] = True if v not in edges[center]: connect(center, v) for u in edges[v]: if not visited[u]: queue.append(u) for i in range(1, center): redundant = edges[i] - target[i] - {center} lacking = target[i] - edges[i] for l in lacking: connect(i, l) for r in redundant: disconnect(i, r) for i in range(center+1, n+1): redundant = edges[i] - target[i] - {center} lacking = target[i] - edges[i] for l in lacking: connect(i, l) for r in redundant: disconnect(i, r) visited = [False] * (n + 1) start = 1 for i in range(2, n + 1): if len(target[i]) == 1: start = i break visited[start] = True queue = list(target[start]) while queue: v = queue.pop(0) visited[v] = True if v not in target[center] and v in edges[center]: disconnect(center, v) for u in target[v]: if not visited[u]: queue.append(u) print(len(moves)) for m in moves: print(m) |