結果

問題 No.1553 Lovely City
ユーザー neterukunneterukun
提出日時 2021-06-18 21:50:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,566 ms / 2,000 ms
コード長 3,605 bytes
コンパイル時間 1,127 ms
コンパイル使用メモリ 86,740 KB
実行使用メモリ 244,788 KB
最終ジャッジ日時 2023-09-04 22:49:58
合計ジャッジ時間 34,337 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,176 KB
testcase_01 AC 71 ms
71,268 KB
testcase_02 AC 395 ms
164,096 KB
testcase_03 AC 79 ms
71,308 KB
testcase_04 AC 81 ms
71,256 KB
testcase_05 AC 88 ms
76,024 KB
testcase_06 AC 85 ms
76,472 KB
testcase_07 AC 79 ms
75,320 KB
testcase_08 AC 1,062 ms
173,676 KB
testcase_09 AC 1,174 ms
181,952 KB
testcase_10 AC 1,263 ms
200,308 KB
testcase_11 AC 982 ms
170,744 KB
testcase_12 AC 1,222 ms
206,964 KB
testcase_13 AC 1,007 ms
157,064 KB
testcase_14 AC 1,080 ms
164,012 KB
testcase_15 AC 1,072 ms
174,088 KB
testcase_16 AC 1,163 ms
176,312 KB
testcase_17 AC 1,073 ms
177,448 KB
testcase_18 AC 1,566 ms
223,552 KB
testcase_19 AC 1,535 ms
239,748 KB
testcase_20 AC 1,522 ms
230,968 KB
testcase_21 AC 1,504 ms
235,460 KB
testcase_22 AC 1,550 ms
243,252 KB
testcase_23 AC 1,535 ms
244,788 KB
testcase_24 AC 1,520 ms
232,964 KB
testcase_25 AC 1,497 ms
231,836 KB
testcase_26 AC 1,512 ms
237,628 KB
testcase_27 AC 1,519 ms
235,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline


class UnionFind:
    def __init__(self, n):
        self.parent = [-1] * n
        self.n = n
        self.cnt = n

    def root(self, x):
        if self.parent[x] < 0:
            return x
        else:
            self.parent[x] = self.root(self.parent[x])
            return self.parent[x]

    def merge(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return False
        if self.parent[x] > self.parent[y]:
            x, y = y, x
        self.parent[x] += self.parent[y]
        self.parent[y] = x
        self.cnt -= 1
        return True

    def same(self, x, y):
        return self.root(x) == self.root(y)

    def size(self, x):
        return -self.parent[self.root(x)]

    def count(self):
        return self.cnt

    def groups(self):
        res = [[] for _ in range(self.n)]
        for i in range(self.n):
            res[self.root(i)].append(i)
        return [group for group in res if group]


class StronglyConnectedComponents:
    def __init__(self, n):
        self.n = n
        self.graph = [[] for _ in range(n)]
        self.rev_graph = [[] for _ in range(n)]
        self.labels = [-1] * n
        self.lb_cnt = 0

    def add_edge(self, v, nxt_v):
        self.graph[v].append(nxt_v)
        self.rev_graph[nxt_v].append(v)

    def build(self):
        self.post_order = []
        self.used = [False] * self.n
        for v in range(self.n):
            if not self.used[v]:
                self._dfs(v)

        for v in reversed(self.post_order):
            if self.labels[v] == -1:
                self._rev_dfs(v)
                self.lb_cnt += 1

    def _dfs(self, v):
        stack = [v, 0]
        while stack:
            v, idx = stack[-2:]
            if not idx and self.used[v]:
                stack.pop()
                stack.pop()
            else:
                self.used[v] = True
                if idx < len(self.graph[v]):
                    stack[-1] += 1
                    stack.append(self.graph[v][idx])
                    stack.append(0)
                else:
                    stack.pop()
                    self.post_order.append(stack.pop())

    def _rev_dfs(self, v):
        stack = [v]
        self.labels[v] = self.lb_cnt
        while stack:
            v = stack.pop()
            for nxt_v in self.rev_graph[v]:
                if self.labels[nxt_v] != -1:
                    continue
                stack.append(nxt_v)
                self.labels[nxt_v] = self.lb_cnt

    def construct_dag(self):
        self.dag = [[] for i in range(self.lb_cnt)]
        self.groups = [[] for i in range(self.lb_cnt)]
        for v, lb in enumerate(self.labels):
            for nxt_v in self.graph[v]:
                nxt_lb = self.labels[nxt_v]
                if lb == nxt_lb:
                    continue
                self.dag[lb].append(nxt_lb)
            self.groups[lb].append(v)
        return self.dag, self.groups


n, m = map(int, input().split())
edges = [list(map(int, input().split())) for i in range(m)]

    
scc = StronglyConnectedComponents(n)
uf = UnionFind(n)
for u, v in edges:
    u -= 1
    v -= 1
    scc.add_edge(u, v)
    uf.merge(u, v)

scc.build()
dag, group = scc.construct_dag()


ans = []
for gp in uf.groups():
    gp = sorted(gp, key=lambda x: scc.labels[x])
    lb = [scc.labels[i] for i in gp]
    for i in range(len(gp) - 1):
        ans.append((gp[i] + 1, gp[i + 1] + 1))
    if len(set(lb)) != len(lb):
        ans.append((gp[-1] + 1, gp[0] + 1))

print(len(ans))
for res in ans:
    print(*res)
0