結果

問題 No.1194 Replace
ユーザー tktk_snsntktk_snsn
提出日時 2021-02-08 22:08:39
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 877 bytes
コンパイル時間 513 ms
コンパイル使用メモリ 10,880 KB
実行使用メモリ 816,864 KB
最終ジャッジ日時 2023-09-20 05:14:01
合計ジャッジ時間 8,297 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 MLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

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

val = set()
for b, c in BC:
    val.add(b)
    val.add(c)

val = sorted(val)
vtoi = {v: i for i, v in enumerate(val)}
sz = len(val)
edge = [[] for _ in range(sz)]
for b, c in BC:
    s = vtoi[b]
    t = vtoi[c]
    edge[s].append(t)


def dfs(s, p, val):
    vis[s] = 1
    for t in edge[s]:
        if vis[t]:
            val = max(val, goal[t])
            continue
        if t == p:
            continue
        dfs(t, s, val)
        val = max(val, goal[t])
    goal[s] = max(goal[s], val)


vis = [0] * N
goal = list(range(sz))
for i in range(sz):
    if vis[i]:
        continue
    dfs(i, -1, i)

ans = N * (N + 1) // 2
for i, g in enumerate(goal):
    ans -= val[i]
    ans += val[g]
print(ans)
0