結果
問題 | No.1254 補強への架け橋 |
ユーザー | Kazun |
提出日時 | 2024-02-12 10:57:48 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 491 ms / 2,000 ms |
コード長 | 8,571 bytes |
コンパイル時間 | 258 ms |
コンパイル使用メモリ | 82,540 KB |
実行使用メモリ | 130,804 KB |
最終ジャッジ日時 | 2024-09-28 17:54:24 |
合計ジャッジ時間 | 28,918 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 38 ms
56,796 KB |
testcase_01 | AC | 37 ms
56,020 KB |
testcase_02 | AC | 40 ms
55,776 KB |
testcase_03 | AC | 40 ms
56,188 KB |
testcase_04 | AC | 40 ms
56,684 KB |
testcase_05 | AC | 39 ms
55,860 KB |
testcase_06 | AC | 41 ms
56,300 KB |
testcase_07 | AC | 39 ms
55,628 KB |
testcase_08 | AC | 39 ms
56,504 KB |
testcase_09 | AC | 40 ms
57,064 KB |
testcase_10 | AC | 40 ms
55,680 KB |
testcase_11 | AC | 40 ms
56,072 KB |
testcase_12 | AC | 40 ms
55,860 KB |
testcase_13 | AC | 41 ms
56,844 KB |
testcase_14 | AC | 40 ms
56,892 KB |
testcase_15 | AC | 40 ms
56,384 KB |
testcase_16 | AC | 40 ms
57,344 KB |
testcase_17 | AC | 42 ms
56,244 KB |
testcase_18 | AC | 40 ms
55,624 KB |
testcase_19 | AC | 41 ms
56,568 KB |
testcase_20 | AC | 40 ms
56,600 KB |
testcase_21 | AC | 39 ms
56,012 KB |
testcase_22 | AC | 41 ms
56,956 KB |
testcase_23 | AC | 38 ms
56,768 KB |
testcase_24 | AC | 40 ms
57,796 KB |
testcase_25 | AC | 40 ms
57,124 KB |
testcase_26 | AC | 42 ms
56,092 KB |
testcase_27 | AC | 40 ms
56,864 KB |
testcase_28 | AC | 40 ms
56,516 KB |
testcase_29 | AC | 40 ms
56,200 KB |
testcase_30 | AC | 41 ms
56,848 KB |
testcase_31 | AC | 42 ms
57,188 KB |
testcase_32 | AC | 42 ms
55,932 KB |
testcase_33 | AC | 41 ms
56,780 KB |
testcase_34 | AC | 39 ms
56,256 KB |
testcase_35 | AC | 40 ms
57,060 KB |
testcase_36 | AC | 39 ms
56,224 KB |
testcase_37 | AC | 42 ms
57,236 KB |
testcase_38 | AC | 44 ms
56,892 KB |
testcase_39 | AC | 39 ms
57,544 KB |
testcase_40 | AC | 40 ms
57,552 KB |
testcase_41 | AC | 39 ms
56,504 KB |
testcase_42 | AC | 39 ms
57,664 KB |
testcase_43 | AC | 69 ms
74,960 KB |
testcase_44 | AC | 45 ms
59,184 KB |
testcase_45 | AC | 42 ms
58,016 KB |
testcase_46 | AC | 60 ms
73,072 KB |
testcase_47 | AC | 46 ms
60,296 KB |
testcase_48 | AC | 61 ms
71,676 KB |
testcase_49 | AC | 43 ms
56,848 KB |
testcase_50 | AC | 75 ms
77,396 KB |
testcase_51 | AC | 67 ms
75,664 KB |
testcase_52 | AC | 75 ms
77,336 KB |
testcase_53 | AC | 58 ms
71,816 KB |
testcase_54 | AC | 67 ms
74,760 KB |
testcase_55 | AC | 64 ms
72,404 KB |
testcase_56 | AC | 46 ms
57,060 KB |
testcase_57 | AC | 65 ms
71,792 KB |
testcase_58 | AC | 57 ms
69,888 KB |
testcase_59 | AC | 43 ms
58,124 KB |
testcase_60 | AC | 48 ms
58,544 KB |
testcase_61 | AC | 68 ms
74,352 KB |
testcase_62 | AC | 43 ms
56,836 KB |
testcase_63 | AC | 144 ms
81,368 KB |
testcase_64 | AC | 120 ms
79,284 KB |
testcase_65 | AC | 137 ms
79,748 KB |
testcase_66 | AC | 132 ms
79,440 KB |
testcase_67 | AC | 116 ms
78,604 KB |
testcase_68 | AC | 131 ms
79,628 KB |
testcase_69 | AC | 133 ms
80,084 KB |
testcase_70 | AC | 127 ms
79,128 KB |
testcase_71 | AC | 116 ms
79,204 KB |
testcase_72 | AC | 147 ms
80,644 KB |
testcase_73 | AC | 128 ms
78,868 KB |
testcase_74 | AC | 132 ms
79,908 KB |
testcase_75 | AC | 128 ms
79,272 KB |
testcase_76 | AC | 98 ms
78,664 KB |
testcase_77 | AC | 131 ms
79,148 KB |
testcase_78 | AC | 151 ms
80,780 KB |
testcase_79 | AC | 145 ms
81,480 KB |
testcase_80 | AC | 146 ms
80,800 KB |
testcase_81 | AC | 155 ms
81,560 KB |
testcase_82 | AC | 143 ms
80,688 KB |
testcase_83 | AC | 491 ms
129,908 KB |
testcase_84 | AC | 452 ms
127,664 KB |
testcase_85 | AC | 315 ms
109,168 KB |
testcase_86 | AC | 413 ms
120,384 KB |
testcase_87 | AC | 446 ms
125,108 KB |
testcase_88 | AC | 166 ms
83,620 KB |
testcase_89 | AC | 474 ms
130,140 KB |
testcase_90 | AC | 322 ms
110,300 KB |
testcase_91 | AC | 290 ms
103,396 KB |
testcase_92 | AC | 197 ms
89,804 KB |
testcase_93 | AC | 384 ms
118,468 KB |
testcase_94 | AC | 359 ms
114,940 KB |
testcase_95 | AC | 357 ms
114,672 KB |
testcase_96 | AC | 457 ms
125,616 KB |
testcase_97 | AC | 253 ms
96,256 KB |
testcase_98 | AC | 457 ms
125,456 KB |
testcase_99 | AC | 290 ms
106,340 KB |
testcase_100 | AC | 483 ms
130,804 KB |
testcase_101 | AC | 192 ms
88,652 KB |
testcase_102 | AC | 155 ms
82,164 KB |
testcase_103 | AC | 186 ms
89,664 KB |
testcase_104 | AC | 204 ms
94,816 KB |
testcase_105 | AC | 352 ms
118,624 KB |
testcase_106 | AC | 232 ms
100,284 KB |
testcase_107 | AC | 437 ms
130,096 KB |
testcase_108 | AC | 418 ms
130,188 KB |
testcase_109 | AC | 359 ms
119,288 KB |
testcase_110 | AC | 317 ms
114,284 KB |
testcase_111 | AC | 329 ms
116,056 KB |
testcase_112 | AC | 193 ms
93,964 KB |
testcase_113 | AC | 333 ms
113,436 KB |
testcase_114 | AC | 235 ms
97,456 KB |
testcase_115 | AC | 157 ms
83,380 KB |
testcase_116 | AC | 255 ms
103,784 KB |
testcase_117 | AC | 205 ms
91,916 KB |
testcase_118 | AC | 415 ms
126,320 KB |
testcase_119 | AC | 283 ms
106,596 KB |
testcase_120 | AC | 397 ms
124,180 KB |
testcase_121 | AC | 181 ms
91,216 KB |
testcase_122 | AC | 256 ms
103,456 KB |
testcase_123 | AC | 39 ms
57,272 KB |
testcase_124 | AC | 419 ms
119,504 KB |
testcase_125 | AC | 412 ms
119,308 KB |
ソースコード
class Graph: __slots__=("edge_offset", "edge_alive", "edge_ids", "vertex_alive", "adjacent", "deg") #入力定義 def __init__(self, N = 0, edge_offset = 0): """ N 頂点の空グラフ (多重辺なし) を生成する.""" self.adjacent = [[] for _ in range(N)] self.edge_ids = [[] for _ in range(N)] self.vertex_alive = [1] * N self.edge_offset = edge_offset self.edge_alive = [0] * edge_offset self.deg = [0] * N #頂点の追加 def add_vertex(self): """ 頂点を追加する. """ self.adjacent.append([]) self.edge_ids.append([]) self.vertex_alive.append(1) self.deg.append(0) return self.order() - 1 def add_vertices(self, k): """ 頂点を k 個追加する. k: int """ n=self.order() self.adjacent.extend([[] for _ in range(k)]) self.edge_ids.extend([[] for _ in range(k)]) self.vertex_alive.extend([1] * k) self.deg.extend([0] * k) return list(range(n, n + k)) #辺の追加 def add_edge(self, u, v): """ 無向辺 uv を加える""" j = len(self.edge_alive) self.adjacent[u].append(v) self.adjacent[v].append(u) self.edge_ids[u].append(j) self.edge_ids[v].append(j) self.deg[u] += 1; self.deg[v] += 1 self.edge_alive.append(1) return j #辺を除く def remove_edge(self,u,v): """ 無向辺 uv が存在するならば除く""" pass def reset_vertex(self, u): """ 頂点 u に接続している辺を全て消す.""" pass #Walkの追加 def add_walk(self, *walk): """ walk=(w[0],...,w[n-1]) に対して, n-1 本の辺 w[i]w[i+1] を加える.""" for i in range(len(walk) - 1): self.add_edge(walk[i], walk[i + 1]) #Cycleの追加 def add_cycle(self, *cycle): """ cycle=(c[0], ..., c[n-1]) を加える. """ self.add_walk(*cycle) self.add_edge(cycle[-1], cycle[0]) #グラフに辺が存在するか否か def edge_exist(self, u, v): """ 辺 uv が存在するか? """ pass def partner(self, v): adj = self.adjacent[v] edge_ids = self.edge_ids[v] return [adj[k] for k in range(len(edge_ids)) if self.edge_alive[edge_ids[k]]] def partner_with_index(self, v): adj = self.adjacent[v] edge_ids = self.edge_ids[v] return [(adj[k], edge_ids[k]) for k in range(len(edge_ids)) if self.edge_alive[edge_ids[k]]] #近傍 def neighborhood(self, v): """ 頂点 v の近傍を求める. """ adj = self.adjacent[v] edge_ids = self.edge_ids[v] return list(set(adj[k] for k in range(len(edge_ids)) if self.edge_alive[edge_ids[k]])) #次数 def degree(self, v): """ 頂点 v の次数を求める. """ return self.deg[v] #頂点数 def vertex_count(self): """ グラフの頂点数 (位数) を出力する. """ return len(self.adjacent) def order(self): """ グラフの位数 (頂点数) を出力する. """ return len(self.adjacent) #辺数 def edge_count(self): """ 辺の本数 (サイズ) を出力する.""" return len(self.edge_alive) - self.edge_offset def size(self): """ サイズ (辺の本数) を出力する. """ return len(self.edge_alive) - self.edge_offset #頂点vを含む連結成分 def connected_component(self, v): """ 頂点 v を含む連結成分を出力する.""" N = self.order() stack = [v] comp = [0] * N; comp[v] = 1 while stack: x = stack.pop() for y in self.neighborhood(x): if comp[y] == 0: comp[y] = 1 stack.append(y) return [x for x in range(N) if comp[x]] #距離 def distance(self, u, v, default): """ 2頂点 u,v 間の距離を求める.""" if u == v: return 0 from collections import deque N = self.order() dist = [-1] * N; dist[u]=0 queue = deque([u]) while queue: x = queue.popleft() for y in self.neighborhood(x): if dist[y] == -1: dist[y] = dist[x] + 1 queue.append(y) if y == v: return dist[v] return default #ある1点からの距離 def distance_all(self,u,default=-1): """ 頂点 u からの距離を求める.""" from collections import deque N = self.order() dist = [-1] * N; dist[u]=0 queue = deque([u]) while queue: x = queue.popleft() for y in self.neighborhood(x): if dist[y] == -1: dist[y] = dist[x] + 1 queue.append(y) return [dist[x] if dist[x] != -1 else default for x in range(N)] #最短路 def shortest_path(self, u, v): """ u から v への最短路を求める (存在しない場合は None). """ if u == v: return [u] from collections import deque prev = [-1] * self.order() prev[u] = u queue = deque([u]) while queue: x = queue.popleft() for y in self.adjacent[x]: if prev[x] != -1: continue prev[y] = x queue.append(y) if y != v: continue path = [v] a = v while a != u: a = prev[a] path.append(a) return path[::-1] return None def edge_yielder(self): u = [0] * len(self.edge_alive); v = [0] * len(self.edge_alive) for x in range(self.order()): adj = self.adjacent[x] ids = self.edge_ids[x] for k in range(len(adj)): id = ids[k] u[id] = min(x, adj[k]) v[id] = max(x, adj[k]) for id in range(len(self.edge_alive)): if self.edge_alive[id]: yield (u[id], v[id]) def edge_yielder_with_index(self): u = [0] * len(self.edge_alive); v = [0] * len(self.edge_alive) for x in range(self.order()): adj = self.adjacent[x] ids = self.edge_ids[x] for k in range(len(adj)): id = ids[k] u[id] = min(x, adj[k]) v[id] = max(x, adj[k]) for id in range(len(self.edge_alive)): if self.edge_alive[id]: yield (id, u[id], v[id]) def Lowlink(G: Graph, mode=0): """ G の ord, lowlink を求める. G: Graph output: (ord, lowlink) """ from collections import deque N=G.vertex_count() ord=[-1]*N; low=[-1]*N flag=[0]*N adj=G.adjacent parent=[-1]*N #BFSパート for v in range(N): if flag[v]: continue k=0 S=deque([v]) T=[] while S: u=S.pop() if flag[u]: continue T.append(u) ord[u]=k k+=1 flag[u]=1 for w in G.neighborhood(u): if not flag[w]: S.append(w) parent[w]=u for u in T: low[u]=ord[u] for w in T[:0:-1]: for x in adj[w]: if w==v or x!=parent[w]: low[w]=min(low[w],low[x],ord[x]) if mode==0: return ord, low else: return ord, low, parent #橋列挙 def Bridge(G: Graph): """ G にある橋の id を列挙する. G: Graph """ ord, low = Lowlink(G) return [id for id, u, v in G.edge_yielder_with_index() if (ord[u] < low[v]) or (ord[v] < low[u])] #================================================== def solve(): N = int(input()) G = Graph(N + 1, 1) for j in range(1, N + 1): u, v = map(int, input().split()) G.add_edge(u, v) bridges = set(Bridge(G)) anti_bridges = [j for j in range(1, N + 1) if j not in bridges] return f'{len(anti_bridges)}\n{" ".join(map(str, anti_bridges))}' #================================================== import sys input=sys.stdin.readline write=sys.stdout.write print(solve())