結果
問題 | No.1553 Lovely City |
ユーザー | rlangevin |
提出日時 | 2023-12-02 01:14:02 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,227 ms / 2,000 ms |
コード長 | 2,054 bytes |
コンパイル時間 | 443 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 165,944 KB |
最終ジャッジ日時 | 2024-09-26 16:11:07 |
合計ジャッジ時間 | 27,055 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 48 ms
54,784 KB |
testcase_01 | AC | 48 ms
54,528 KB |
testcase_02 | AC | 291 ms
165,944 KB |
testcase_03 | AC | 62 ms
62,080 KB |
testcase_04 | AC | 66 ms
63,616 KB |
testcase_05 | AC | 75 ms
66,944 KB |
testcase_06 | AC | 58 ms
62,080 KB |
testcase_07 | AC | 60 ms
62,592 KB |
testcase_08 | AC | 813 ms
128,032 KB |
testcase_09 | AC | 836 ms
130,816 KB |
testcase_10 | AC | 1,019 ms
142,208 KB |
testcase_11 | AC | 777 ms
125,696 KB |
testcase_12 | AC | 1,047 ms
148,468 KB |
testcase_13 | AC | 713 ms
116,940 KB |
testcase_14 | AC | 832 ms
133,484 KB |
testcase_15 | AC | 897 ms
128,896 KB |
testcase_16 | AC | 871 ms
131,992 KB |
testcase_17 | AC | 880 ms
128,476 KB |
testcase_18 | AC | 1,173 ms
154,840 KB |
testcase_19 | AC | 1,184 ms
155,016 KB |
testcase_20 | AC | 1,190 ms
154,412 KB |
testcase_21 | AC | 1,197 ms
154,552 KB |
testcase_22 | AC | 1,199 ms
153,676 KB |
testcase_23 | AC | 1,210 ms
155,016 KB |
testcase_24 | AC | 1,214 ms
154,604 KB |
testcase_25 | AC | 1,197 ms
154,620 KB |
testcase_26 | AC | 1,227 ms
155,076 KB |
testcase_27 | AC | 1,196 ms
155,752 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)