結果

問題 No.1194 Replace
ユーザー tktk_snsntktk_snsn
提出日時 2021-02-08 23:14:56
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 850 bytes
コンパイル時間 207 ms
コンパイル使用メモリ 11,104 KB
実行使用メモリ 192,900 KB
最終ジャッジ日時 2023-09-20 08:02:13
合計ジャッジ時間 34,688 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,398 ms
123,468 KB
testcase_01 AC 1,471 ms
129,860 KB
testcase_02 AC 1,170 ms
109,724 KB
testcase_03 AC 1,031 ms
97,176 KB
testcase_04 AC 1,538 ms
129,856 KB
testcase_05 AC 1,429 ms
122,404 KB
testcase_06 AC 1,345 ms
115,808 KB
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 AC 1,061 ms
86,160 KB
testcase_14 AC 896 ms
76,560 KB
testcase_15 AC 795 ms
71,344 KB
testcase_16 AC 1,000 ms
84,324 KB
testcase_17 AC 736 ms
66,712 KB
testcase_18 AC 697 ms
64,768 KB
testcase_19 AC 1,090 ms
85,064 KB
testcase_20 AC 17 ms
8,368 KB
testcase_21 AC 17 ms
8,228 KB
testcase_22 AC 16 ms
8,192 KB
testcase_23 AC 326 ms
36,620 KB
testcase_24 AC 114 ms
20,376 KB
testcase_25 AC 45 ms
11,956 KB
testcase_26 AC 356 ms
36,516 KB
testcase_27 AC 64 ms
14,008 KB
testcase_28 AC 158 ms
22,068 KB
testcase_29 AC 32 ms
10,264 KB
権限があれば一括ダウンロードができます

ソースコード

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)
G = [set() for _ in range(sz)]
for b, c in BC:
    b = vtoi[b]
    c = vtoi[c]
    G[c].add(b)


def dfs(s, p):
    vis[s] = 1
    for t in G[s]:
        if t == p:
            continue
        if vis[t]:
            continue
        if score[t] >= score[s]:
            continue
        score[t] = score[s]
        dfs(t, s)


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

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