結果
問題 | No.1553 Lovely City |
ユーザー | mkawa2 |
提出日時 | 2023-01-12 00:32:53 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 692 ms / 2,000 ms |
コード長 | 2,341 bytes |
コンパイル時間 | 263 ms |
コンパイル使用メモリ | 82,404 KB |
実行使用メモリ | 154,624 KB |
最終ジャッジ日時 | 2024-06-02 01:56:10 |
合計ジャッジ時間 | 17,274 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 33 ms
52,480 KB |
testcase_01 | AC | 39 ms
53,120 KB |
testcase_02 | AC | 89 ms
98,648 KB |
testcase_03 | AC | 39 ms
54,272 KB |
testcase_04 | AC | 40 ms
54,912 KB |
testcase_05 | AC | 45 ms
61,184 KB |
testcase_06 | AC | 46 ms
62,208 KB |
testcase_07 | AC | 40 ms
54,656 KB |
testcase_08 | AC | 472 ms
118,916 KB |
testcase_09 | AC | 517 ms
122,452 KB |
testcase_10 | AC | 550 ms
134,852 KB |
testcase_11 | AC | 435 ms
116,072 KB |
testcase_12 | AC | 558 ms
137,808 KB |
testcase_13 | AC | 469 ms
113,664 KB |
testcase_14 | AC | 505 ms
120,400 KB |
testcase_15 | AC | 570 ms
122,624 KB |
testcase_16 | AC | 526 ms
125,284 KB |
testcase_17 | AC | 447 ms
120,704 KB |
testcase_18 | AC | 674 ms
153,296 KB |
testcase_19 | AC | 675 ms
151,628 KB |
testcase_20 | AC | 672 ms
153,156 KB |
testcase_21 | AC | 688 ms
154,524 KB |
testcase_22 | AC | 692 ms
149,936 KB |
testcase_23 | AC | 676 ms
147,236 KB |
testcase_24 | AC | 663 ms
145,364 KB |
testcase_25 | AC | 671 ms
154,624 KB |
testcase_26 | AC | 681 ms
151,228 KB |
testcase_27 | AC | 680 ms
152,676 KB |
ソースコード
import sys # sys.setrecursionlimit(1000005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() # dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = (1 << 63)-1 # inf = (1 << 31)-1 md = 10**9+7 # md = 998244353 class UnionFind: def __init__(self, n): self.table = [-1]*n self.mem = [[u] for u in range(n)] self.cnt = n def root(self, u): stack = [] while self.table[u] >= 0: stack.append(u) u = self.table[u] for v in stack: self.table[v] = u return u def same(self, u, v): return self.root(u) == self.root(v) def merge(self, u, v): u = self.root(u) v = self.root(v) if u == v: return False su = -self.table[u] sv = -self.table[v] if su < sv: u, v = v, u self.table[u] = -su-sv self.table[v] = u self.mem[u] += self.mem[v] self.mem[v].clear() self.cnt -= 1 return True def member(self, u): return self.mem[self.root(u)] # グループの要素数 def size(self, u): return -self.table[self.root(u)] n, m = LI() to = [[] for _ in range(n)] deg = [0]*n uf = UnionFind(n) for _ in range(m): u, v = LI1() to[u].append(v) deg[v] += 1 uf.merge(u, v) ans = [] for uu in uf.mem: if len(uu) == 1: continue topo = [] st = [] for u in uu: if deg[u] == 0: st.append(u) while st: u = st.pop() topo.append(u) for v in to[u]: deg[v] -= 1 if deg[v] == 0: st.append(v) if len(topo) == len(uu): for i in range(len(topo)-1): ans.append((topo[i], topo[i+1])) else: for i in range(len(uu)): ans.append((uu[i-1], uu[i])) print(len(ans)) for u, v in ans: print(u+1, v+1)