結果

問題 No.1553 Lovely City
ユーザー H3PO4H3PO4
提出日時 2022-11-26 15:42:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,105 ms / 2,000 ms
コード長 2,662 bytes
コンパイル時間 205 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 190,384 KB
最終ジャッジ日時 2024-10-02 22:18:01
合計ジャッジ時間 25,409 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
54,272 KB
testcase_01 AC 47 ms
54,400 KB
testcase_02 AC 141 ms
123,724 KB
testcase_03 AC 49 ms
56,448 KB
testcase_04 AC 54 ms
62,720 KB
testcase_05 AC 62 ms
67,200 KB
testcase_06 AC 63 ms
68,352 KB
testcase_07 AC 57 ms
64,256 KB
testcase_08 AC 727 ms
143,168 KB
testcase_09 AC 810 ms
159,192 KB
testcase_10 AC 825 ms
155,340 KB
testcase_11 AC 667 ms
139,500 KB
testcase_12 AC 849 ms
163,452 KB
testcase_13 AC 671 ms
152,844 KB
testcase_14 AC 732 ms
157,688 KB
testcase_15 AC 745 ms
144,804 KB
testcase_16 AC 831 ms
157,948 KB
testcase_17 AC 675 ms
141,364 KB
testcase_18 AC 1,065 ms
178,420 KB
testcase_19 AC 1,055 ms
177,020 KB
testcase_20 AC 1,105 ms
177,108 KB
testcase_21 AC 1,071 ms
180,256 KB
testcase_22 AC 1,027 ms
182,436 KB
testcase_23 AC 1,033 ms
175,676 KB
testcase_24 AC 1,069 ms
190,384 KB
testcase_25 AC 1,040 ms
185,716 KB
testcase_26 AC 1,089 ms
178,780 KB
testcase_27 AC 1,040 ms
189,916 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