from collections import defaultdict def get_input(): n = int(input()) ms = int(input()) src_bonds = set() dst_bonds = set() graph = defaultdict(set) for _ in range(ms): a, b = [int(x) for x in input().split(' ')] if a < b: src_bonds.add((a, b)) else: src_bonds.add((b, a)) graph[a].add(b) graph[b].add(a) md = int(input()) for _ in range(md): a, b = [int(x) for x in input().split(' ')] if a < b: dst_bonds.add((a, b)) else: dst_bonds.add((b, a)) return n, ms, src_bonds, md, dst_bonds, graph def get_can_add(graph): can_add = set() for v, neighbours in graph.items(): for u in neighbours: for k in graph[u]: if v < k: can_add.add((v, k)) return can_add # def heuristic_add(to_add, graph, can_add, operations): # want_to_add = to_add & can_add # while len(want_to_add) > 0: # for u, v in want_to_add: # graph[u].add(v) # graph[v].add(u) # operations.append((f'+ {u} {v}')) # can_add.remove((u, v)) # for k in graph[v]: # if u < k: # can_add.add((u, k)) # for k in graph[u]: # if v < k: # can_add.add((v, k)) # want_to_add = to_add & can_add # def add_edges(src_bonds, dst_bonds, graph, can_add, operations): # to_add = dst_bonds - src_bonds # heuristic_add(to_add, graph, can_add, operations) def add_edge(src_bonds, dst_bonds, to_add, to_remove, graph, can_add, operations, u, v): if (u, v) not in src_bonds: if (u, v) in to_add: to_add.remove((u, v)) if (u, v) not in dst_bonds: to_remove.add((u, v)) graph[u].add(v) graph[v].add(u) src_bonds.add((u, v)) operations.append(f'+ {u} {v}') for k in graph[v]: if u < k: can_add.add((u, k)) for k in graph[u]: if v < k: can_add.add((v, k)) n, ms, src_bonds, md, dst_bonds, graph = get_input() can_add = get_can_add(graph) to_add = dst_bonds - src_bonds to_remove = src_bonds - dst_bonds operations = [] while to_add: for u, v in can_add.copy(): add_edge(src_bonds, dst_bonds, to_add, to_remove, graph, can_add, operations, u, v) for (u, v) in to_remove: operations.append(f'- {u} {v}') print(len(operations)) for o in operations: print(o)
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | from collections import defaultdict def get_input(): n = int(input()) ms = int(input()) src_bonds = set() dst_bonds = set() graph = defaultdict(set) for _ in range(ms): a, b = [int(x) for x in input().split(' ')] if a < b: src_bonds.add((a, b)) else: src_bonds.add((b, a)) graph[a].add(b) graph[b].add(a) md = int(input()) for _ in range(md): a, b = [int(x) for x in input().split(' ')] if a < b: dst_bonds.add((a, b)) else: dst_bonds.add((b, a)) return n, ms, src_bonds, md, dst_bonds, graph def get_can_add(graph): can_add = set() for v, neighbours in graph.items(): for u in neighbours: for k in graph[u]: if v < k: can_add.add((v, k)) return can_add # def heuristic_add(to_add, graph, can_add, operations): # want_to_add = to_add & can_add # while len(want_to_add) > 0: # for u, v in want_to_add: # graph[u].add(v) # graph[v].add(u) # operations.append((f'+ {u} {v}')) # can_add.remove((u, v)) # for k in graph[v]: # if u < k: # can_add.add((u, k)) # for k in graph[u]: # if v < k: # can_add.add((v, k)) # want_to_add = to_add & can_add # def add_edges(src_bonds, dst_bonds, graph, can_add, operations): # to_add = dst_bonds - src_bonds # heuristic_add(to_add, graph, can_add, operations) def add_edge(src_bonds, dst_bonds, to_add, to_remove, graph, can_add, operations, u, v): if (u, v) not in src_bonds: if (u, v) in to_add: to_add.remove((u, v)) if (u, v) not in dst_bonds: to_remove.add((u, v)) graph[u].add(v) graph[v].add(u) src_bonds.add((u, v)) operations.append(f'+ {u} {v}') for k in graph[v]: if u < k: can_add.add((u, k)) for k in graph[u]: if v < k: can_add.add((v, k)) n, ms, src_bonds, md, dst_bonds, graph = get_input() can_add = get_can_add(graph) to_add = dst_bonds - src_bonds to_remove = src_bonds - dst_bonds operations = [] while to_add: for u, v in can_add.copy(): add_edge(src_bonds, dst_bonds, to_add, to_remove, graph, can_add, operations, u, v) for (u, v) in to_remove: operations.append(f'- {u} {v}') print(len(operations)) for o in operations: print(o) |