結果

問題 No.1194 Replace
ユーザー tktk_snsntktk_snsn
提出日時 2021-02-08 22:12:33
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 879 bytes
コンパイル時間 687 ms
コンパイル使用メモリ 10,748 KB
実行使用メモリ 135,256 KB
最終ジャッジ日時 2023-09-20 05:32:36
合計ジャッジ時間 33,844 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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)

vis = [0] * sz
goal = list(range(sz))


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)


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