結果

問題 No.1553 Lovely City
ユーザー H3PO4H3PO4
提出日時 2022-11-26 15:42:44
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 1,223 ms / 2,000 ms
コード長 2,662 bytes
コンパイル時間 543 ms
コンパイル使用メモリ 86,712 KB
実行使用メモリ 192,456 KB
最終ジャッジ日時 2023-07-26 05:06:12
合計ジャッジ時間 31,385 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
71,528 KB
testcase_01 AC 100 ms
71,656 KB
testcase_02 AC 200 ms
133,688 KB
testcase_03 AC 105 ms
72,308 KB
testcase_04 AC 111 ms
76,768 KB
testcase_05 AC 117 ms
76,976 KB
testcase_06 AC 121 ms
77,352 KB
testcase_07 AC 113 ms
77,152 KB
testcase_08 AC 827 ms
156,544 KB
testcase_09 AC 873 ms
169,556 KB
testcase_10 AC 929 ms
160,344 KB
testcase_11 AC 754 ms
138,452 KB
testcase_12 AC 964 ms
159,008 KB
testcase_13 AC 771 ms
158,996 KB
testcase_14 AC 819 ms
156,288 KB
testcase_15 AC 813 ms
148,316 KB
testcase_16 AC 909 ms
175,380 KB
testcase_17 AC 784 ms
142,212 KB
testcase_18 AC 1,167 ms
188,252 KB
testcase_19 AC 1,158 ms
191,216 KB
testcase_20 AC 1,176 ms
192,456 KB
testcase_21 AC 1,174 ms
188,976 KB
testcase_22 AC 1,147 ms
180,392 KB
testcase_23 AC 1,176 ms
187,880 KB
testcase_24 AC 1,215 ms
182,524 KB
testcase_25 AC 1,179 ms
178,748 KB
testcase_26 AC 1,223 ms
186,880 KB
testcase_27 AC 1,182 ms
191,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict, deque


class UnionFind:
    __slots__ = ["n", "parent", "height", "size"]

    def __init__(self, n):
        self.n = n
        self.parent = [i for i in range(n)]
        self.height = [1] * n
        self.size = [1] * n

    def find(self, x):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.height[x] < self.height[y]:
                self.parent[x] = y
                self.size[y] += self.size[x]
            else:
                self.parent[y] = x
                self.size[x] += self.size[y]
                if self.height[x] == self.height[y]:
                    self.height[x] += 1

    def issame(self, x, y):
        return self.find(x) == self.find(y)

    def group_size(self, x):
        return self.size[self.find(x)]

    def group_members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parent) if i == x]

    def group_count(self):
        return len(self.roots())


def topological_sort(nodes, edges):
    G = {n: [] for n in nodes}
    deg = {n: 0 for n in nodes}
    tps = []

    for a, b in edges:
        deg[b] += 1
        G[a].append(b)

    d = deque([i for i, x in deg.items() if not x])
    while d:
        v = d.popleft()
        tps.append(v)
        for x in G[v]:
            deg[x] -= 1
            if not deg[x]:
                d.append(x)

    if any(deg.values()):
        return None  # not DAG
    return tps


N, M = map(int, input().split())
uf = UnionFind(N)
edges = tuple(tuple(int(x) - 1 for x in input().split()) for _ in range(M))

for u, v in edges:
    uf.unite(u, v)
cc_edges = defaultdict(list)
cc_nodes = defaultdict(list)
for i in range(N):
    cc_nodes[uf.find(i)].append(i)
for u, v in edges:
    # assert uf.find(u) == uf.find(v)
    cc_edges[uf.find(u)].append((u, v))

ans = []
for root in uf.roots():
    nodes = cc_nodes[root]
    if len(nodes) == 1:
        continue
    edges = cc_edges[root]
    tps = topological_sort(nodes, edges)
    if tps is None:
        # 適当なサイクル
        for i in range(len(nodes)):
            j = i + 1
            if j == len(nodes):
                j = 0
            ans.append((nodes[i], nodes[j]))
    else:
        # DAG
        for i in range(len(tps) - 1):
            ans.append((tps[i], tps[i + 1]))

print(len(ans))
for u, v in ans:
    print(u + 1, v + 1)
0