結果
問題 | No.1553 Lovely City |
ユーザー | rlangevin |
提出日時 | 2023-12-02 01:14:02 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,150 ms / 2,000 ms |
コード長 | 2,054 bytes |
コンパイル時間 | 294 ms |
コンパイル使用メモリ | 81,572 KB |
実行使用メモリ | 165,492 KB |
最終ジャッジ日時 | 2023-12-02 01:14:32 |
合計ジャッジ時間 | 27,299 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 40 ms
55,600 KB |
testcase_01 | AC | 41 ms
55,600 KB |
testcase_02 | AC | 262 ms
165,492 KB |
testcase_03 | AC | 50 ms
63,100 KB |
testcase_04 | AC | 60 ms
65,316 KB |
testcase_05 | AC | 60 ms
67,900 KB |
testcase_06 | AC | 48 ms
63,256 KB |
testcase_07 | AC | 50 ms
63,232 KB |
testcase_08 | AC | 720 ms
127,616 KB |
testcase_09 | AC | 788 ms
130,208 KB |
testcase_10 | AC | 953 ms
141,700 KB |
testcase_11 | AC | 708 ms
125,216 KB |
testcase_12 | AC | 952 ms
149,264 KB |
testcase_13 | AC | 671 ms
116,656 KB |
testcase_14 | AC | 834 ms
132,472 KB |
testcase_15 | AC | 792 ms
128,816 KB |
testcase_16 | AC | 803 ms
131,628 KB |
testcase_17 | AC | 832 ms
128,600 KB |
testcase_18 | AC | 1,088 ms
155,056 KB |
testcase_19 | AC | 1,143 ms
154,420 KB |
testcase_20 | AC | 1,150 ms
153,988 KB |
testcase_21 | AC | 1,112 ms
153,992 KB |
testcase_22 | AC | 1,081 ms
153,212 KB |
testcase_23 | AC | 1,096 ms
154,920 KB |
testcase_24 | AC | 1,118 ms
153,796 KB |
testcase_25 | AC | 1,063 ms
153,860 KB |
testcase_26 | AC | 1,115 ms
154,972 KB |
testcase_27 | AC | 1,126 ms
154,892 KB |
ソースコード
import sys input = sys.stdin.readline from heapq import * from collections import * def topsort(G, s): Q = [] count = defaultdict(int) for u in s: for v in G[u]: count[v] += 1 for u in s: if count[u] == 0: heappush(Q, u) anslst = [] while Q: u = heappop(Q) anslst.append(u) for v in G[u]: count[v] -= 1 if count[v] == 0: heappush(Q, v) return anslst class UnionFind(object): def __init__(self, n=1): self.par = [i for i in range(n)] self.rank = [0 for _ in range(n)] self.size = [1 for _ in range(n)] def find(self, x): if self.par[x] == x: return x else: self.par[x] = self.find(self.par[x]) return self.par[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x != y: if self.rank[x] < self.rank[y]: x, y = y, x if self.rank[x] == self.rank[y]: self.rank[x] += 1 self.par[y] = x self.size[x] += self.size[y] def is_same(self, x, y): return self.find(x) == self.find(y) def get_size(self, x): x = self.find(x) return self.size[x] N, M = map(int, input().split()) G = [[] for i in range(N)] U = UnionFind(N) for i in range(M): u, v = map(int, input().split()) u, v = u - 1, v - 1 U.union(u, v) G[u].append(v) S = [set() for _ in range(N)] for i in range(N): S[U.find(i)].add(i) seen = set() ans = [] for i in range(N): ui = U.find(i) if ui in seen: continue seen.add(ui) L = topsort(G, S[ui]) if U.get_size(i) == len(L): for j in range(len(L) - 1): ans.append((L[j] + 1, L[j + 1] + 1)) else: L = list(S[ui]) for j in range(len(L) - 1): ans.append((L[j] + 1, L[j + 1] + 1)) ans.append((L[-1] + 1, L[0] + 1)) print(len(ans)) for a, b in ans: print(a, b)