結果
問題 | No.1553 Lovely City |
ユーザー | aaaaaaaaaa2230 |
提出日時 | 2021-06-18 22:47:37 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,642 bytes |
コンパイル時間 | 527 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 182,200 KB |
最終ジャッジ日時 | 2024-06-22 21:08:40 |
合計ジャッジ時間 | 31,214 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 41 ms
52,864 KB |
testcase_01 | AC | 41 ms
52,864 KB |
testcase_02 | AC | 143 ms
111,632 KB |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | AC | 54 ms
61,056 KB |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
ソースコード
import sys input = sys.stdin.readline sys.setrecursionlimit(2*10**5) class SCC: def __init__(self, n): self.n = n self.graph = [[] for i in range(n)] self.graph_rev = [[] for i in range(n)] self.used = [False]*n def add_edge(self, fr, to): if fr == to: return self.graph[fr].append(to) self.graph_rev[to].append(fr) def dfs(self, node, graph): self.used[node] = True for nex in graph[node]: if self.used[nex]: continue self.dfs(nex,graph) self.order.append(node) def first_dfs(self): self.used = [False]*self.n self.order = [] for i in range(self.n): if self.used[i]: continue self.dfs(i,self.graph) def second_dfs(self): self.used = [False]*self.n self.ans = [] for node in reversed(self.order): if self.used[node]: continue self.used[node] = True self.order = [] self.dfs(node, self.graph_rev) self.ans.append(self.order) def scc(self): self.first_dfs() self.second_dfs() return self.ans class Unionfind: def __init__(self,n): self.uf = [-1]*n def find(self,x): if self.uf[x] < 0: return x else: self.uf[x] = self.find(self.uf[x]) return self.uf[x] def same(self,x,y): return self.find(x) == self.find(y) def union(self,x,y): x = self.find(x) y = self.find(y) if x == y: return False if self.uf[x] > self.uf[y]: x,y = y,x self.uf[x] += self.uf[y] self.uf[y] = x return True def size(self,x): x = self.find(x) return -self.uf[x] n,m = map(int,input().split()) uf = Unionfind(n) scc = SCC(n) cand = set() for _ in range(m): u,v = map(int,input().split()) u,v = u-1,v-1 uf.union(u,v) scc.add_edge(u,v) cand.add(u) cand.add(v) group = scc.scc() group.append([]) ans = [] for x,y in zip(group,group[1:]): if len(x) != 1: for i in range(len(x)): a = x[i] b = x[(i+1)%len(x)] ans.append((a,b)) if y == []: continue if uf.same(x[0],y[0]): ans.append((x[0],y[0])) if len(cand)+1 >= len(ans): print(len(ans)) for x,y in ans: print(x+1,y+1) else: l = list(cand) print(len(l)+1) for i in range(len(l)): x = l[i] y = l[(i+1)%len(l)] print(x+1,y+1)