import sys from collections import deque class Graph: def __init__(self, n): self.edges = [set() for _ in range(n)] self.targetEdges = [set() for _ in range(n)] self.size = n def addEdge(self, a, b): self.edges[a].add(b) self.edges[b].add(a) def deleteEdge(self, a, b): self.edges[a].remove(b) self.edges[b].remove(a) def addTargetEdge(self, a, b): self.targetEdges[a].add(b) self.targetEdges[b].add(a) def checkHasLink(self, a, b): if not self.edges[a].isdisjoint(self.edges[b]): return -1 for e in self.edges[a]: return e def checkHasLinkTar(self, a, b): return self.targetEdges[a].isdisjoint(self.targetEdges[b]) def checkEdge(self, a, b): return b in self.edges[a] def notInTarget(self, i): return self.edges[i].difference(self.targetEdges[i]) def bfs(self, s): visited = [-1] * self.size leaf = [False] * self.size S = deque() S.append(s) visited[s] = s while S: u = S.pop() f = 0 for v in self.edges[u]: if visited[v] == -1: visited[v] = u S.append(v) f += 1 if f == 0: leaf[u] = True return visited, leaf def dump(self): for i in range(self.size): print(i, self.edges[i]) # for i in range(self.size): # print(i, self.targetEdges[i]) # for i in range(self.size): # print(i, self.edges[i].difference(self.targetEdges[i])) n = int(sys.stdin.readline()) ms = int(sys.stdin.readline()) gr = Graph(n+1) for i in range(ms): a,b = list(map(int, sys.stdin.readline().split())) gr.addEdge(a, b) # gr.dump() md = int(sys.stdin.readline()) to_add = [] for i in range(md): a,b = list(map(int, sys.stdin.readline().split())) if not gr.checkEdge(a, b): to_add.append((a, b)) gr.addTargetEdge(a, b) ops = [] def log(o, a, b): ops.append(o + " " + str(a) + " " + str(b) + "\n") # dodaj nowe extra = [] for a, b in to_add: if gr.checkEdge(a, b): if a < b: extra.remove((a, b)) else: extra.remove((b, a)) continue v = gr.checkHasLink(a, b) if v > 0: gr.addEdge(v, b) log('+', v, b) if v < b: extra.append((v, b)) else: extra.append((b, v)) gr.addEdge(a, b) log('+', a, b) # usun extra jakie dodane dla nowych for a, b in extra: # print('rem', a, b) log('-', a, b) gr.deleteEdge(a, b) # n = 5 # gr = Graph(6) # gr.addEdge(1, 2) # gr.addEdge(1, 3) # gr.addEdge(1, 4) # gr.addEdge(2, 5) hasToDel = False for i in range(1, n+1): if gr.notInTarget(i): hasToDel = True break if hasToDel: # bfs i dodaj extra krawedz do kazdego # gr.dump() visited, leafs = gr.bfs(1) nr = 2 for i in range(2, n+1): if leafs[i]: nr = i if not gr.notInTarget(i): break visited, leafs = gr.bfs(nr) # print(visited) # print(leafs) s = deque() z = [] zs = set() for i in range(1, n+1): if leafs[i]: s.append(i) # print(visited) # print('leafs', s) while s: u = s.popleft() # print(u, gr.checkEdge(nr, u) , visited[u] != nr , u not in z) if visited[u] != nr and u not in zs and u != nr and not gr.checkEdge(nr, u) : z.append(u) zs.add(u) s.append(visited[u]) log('+', nr, u) # print(ops[-1]) # exit(-1) # kasowanie for i in range(1, n+1): nit = gr.notInTarget(i) for v in nit: log('-', i, v) gr.deleteEdge(i, v) for v in z: log('-', nr, v) # gr.dump() sys.stdout.write(str(len(ops))) sys.stdout.write("\n") for l in ops: sys.stdout.write(l) # print(s, a, b) # gr.dump()
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | import sys from collections import deque class Graph: def __init__(self, n): self.edges = [set() for _ in range(n)] self.targetEdges = [set() for _ in range(n)] self.size = n def addEdge(self, a, b): self.edges[a].add(b) self.edges[b].add(a) def deleteEdge(self, a, b): self.edges[a].remove(b) self.edges[b].remove(a) def addTargetEdge(self, a, b): self.targetEdges[a].add(b) self.targetEdges[b].add(a) def checkHasLink(self, a, b): if not self.edges[a].isdisjoint(self.edges[b]): return -1 for e in self.edges[a]: return e def checkHasLinkTar(self, a, b): return self.targetEdges[a].isdisjoint(self.targetEdges[b]) def checkEdge(self, a, b): return b in self.edges[a] def notInTarget(self, i): return self.edges[i].difference(self.targetEdges[i]) def bfs(self, s): visited = [-1] * self.size leaf = [False] * self.size S = deque() S.append(s) visited[s] = s while S: u = S.pop() f = 0 for v in self.edges[u]: if visited[v] == -1: visited[v] = u S.append(v) f += 1 if f == 0: leaf[u] = True return visited, leaf def dump(self): for i in range(self.size): print(i, self.edges[i]) # for i in range(self.size): # print(i, self.targetEdges[i]) # for i in range(self.size): # print(i, self.edges[i].difference(self.targetEdges[i])) n = int(sys.stdin.readline()) ms = int(sys.stdin.readline()) gr = Graph(n+1) for i in range(ms): a,b = list(map(int, sys.stdin.readline().split())) gr.addEdge(a, b) # gr.dump() md = int(sys.stdin.readline()) to_add = [] for i in range(md): a,b = list(map(int, sys.stdin.readline().split())) if not gr.checkEdge(a, b): to_add.append((a, b)) gr.addTargetEdge(a, b) ops = [] def log(o, a, b): ops.append(o + " " + str(a) + " " + str(b) + "\n") # dodaj nowe extra = [] for a, b in to_add: if gr.checkEdge(a, b): if a < b: extra.remove((a, b)) else: extra.remove((b, a)) continue v = gr.checkHasLink(a, b) if v > 0: gr.addEdge(v, b) log('+', v, b) if v < b: extra.append((v, b)) else: extra.append((b, v)) gr.addEdge(a, b) log('+', a, b) # usun extra jakie dodane dla nowych for a, b in extra: # print('rem', a, b) log('-', a, b) gr.deleteEdge(a, b) # n = 5 # gr = Graph(6) # gr.addEdge(1, 2) # gr.addEdge(1, 3) # gr.addEdge(1, 4) # gr.addEdge(2, 5) hasToDel = False for i in range(1, n+1): if gr.notInTarget(i): hasToDel = True break if hasToDel: # bfs i dodaj extra krawedz do kazdego # gr.dump() visited, leafs = gr.bfs(1) nr = 2 for i in range(2, n+1): if leafs[i]: nr = i if not gr.notInTarget(i): break visited, leafs = gr.bfs(nr) # print(visited) # print(leafs) s = deque() z = [] zs = set() for i in range(1, n+1): if leafs[i]: s.append(i) # print(visited) # print('leafs', s) while s: u = s.popleft() # print(u, gr.checkEdge(nr, u) , visited[u] != nr , u not in z) if visited[u] != nr and u not in zs and u != nr and not gr.checkEdge(nr, u) : z.append(u) zs.add(u) s.append(visited[u]) log('+', nr, u) # print(ops[-1]) # exit(-1) # kasowanie for i in range(1, n+1): nit = gr.notInTarget(i) for v in nit: log('-', i, v) gr.deleteEdge(i, v) for v in z: log('-', nr, v) # gr.dump() sys.stdout.write(str(len(ops))) sys.stdout.write("\n") for l in ops: sys.stdout.write(l) # print(s, a, b) # gr.dump() |