結果

問題 No.1194 Replace
ユーザー H3PO4H3PO4
提出日時 2021-12-13 09:16:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 835 ms / 2,000 ms
コード長 715 bytes
コンパイル時間 919 ms
コンパイル使用メモリ 87,108 KB
実行使用メモリ 248,292 KB
最終ジャッジ日時 2023-09-29 09:50:43
合計ジャッジ時間 16,822 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 613 ms
195,424 KB
testcase_01 AC 623 ms
185,772 KB
testcase_02 AC 535 ms
181,744 KB
testcase_03 AC 472 ms
161,636 KB
testcase_04 AC 625 ms
186,804 KB
testcase_05 AC 602 ms
196,384 KB
testcase_06 AC 551 ms
172,856 KB
testcase_07 AC 819 ms
247,448 KB
testcase_08 AC 811 ms
247,784 KB
testcase_09 AC 829 ms
247,444 KB
testcase_10 AC 835 ms
247,836 KB
testcase_11 AC 818 ms
248,292 KB
testcase_12 AC 818 ms
247,372 KB
testcase_13 AC 484 ms
148,904 KB
testcase_14 AC 419 ms
135,932 KB
testcase_15 AC 403 ms
134,556 KB
testcase_16 AC 471 ms
139,848 KB
testcase_17 AC 370 ms
133,760 KB
testcase_18 AC 354 ms
119,288 KB
testcase_19 AC 482 ms
147,364 KB
testcase_20 AC 95 ms
71,792 KB
testcase_21 AC 96 ms
71,796 KB
testcase_22 AC 95 ms
71,484 KB
testcase_23 AC 242 ms
105,740 KB
testcase_24 AC 177 ms
88,556 KB
testcase_25 AC 129 ms
77,992 KB
testcase_26 AC 237 ms
100,512 KB
testcase_27 AC 140 ms
80,848 KB
testcase_28 AC 185 ms
88,168 KB
testcase_29 AC 127 ms
77,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque
input = sys.stdin.buffer.readline

N, M = map(int, input().split())
BC = [tuple(map(int, input().split())) for _ in range(M)]

nodes = set()
for b, c in BC:
    nodes.add(b)
    nodes.add(c)
nodes = sorted(nodes)
comp = {x: i for i, x in enumerate(nodes)}
L = len(comp)

G = [[] for _ in range(L)]
for b, c in BC:
    G[comp[c]].append(comp[b])

table = nodes.copy()
for i in range(L - 1, -1, -1):
    x = nodes[i]
    if x < table[i]:
        continue
    d = deque([i])
    while d:
        v = d.pop()
        for u in G[v]:
            if table[u] < x:
                table[u] = x
                d.append(u)
ans = N * (N + 1) // 2 - sum(nodes) + sum(table)
print(ans)
0