結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,493 ms
123,424 KB
testcase_01 AC 1,555 ms
129,776 KB
testcase_02 AC 1,208 ms
109,768 KB
testcase_03 AC 1,035 ms
97,236 KB
testcase_04 AC 1,557 ms
129,828 KB
testcase_05 AC 1,455 ms
122,212 KB
testcase_06 AC 1,354 ms
115,776 KB
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 AC 1,082 ms
86,136 KB
testcase_14 AC 948 ms
76,580 KB
testcase_15 AC 804 ms
71,352 KB
testcase_16 AC 1,045 ms
84,240 KB
testcase_17 AC 730 ms
66,580 KB
testcase_18 AC 692 ms
64,568 KB
testcase_19 AC 1,035 ms
85,000 KB
testcase_20 AC 16 ms
7,904 KB
testcase_21 AC 17 ms
8,004 KB
testcase_22 AC 16 ms
7,780 KB
testcase_23 AC 307 ms
36,512 KB
testcase_24 AC 110 ms
20,408 KB
testcase_25 AC 47 ms
11,872 KB
testcase_26 AC 344 ms
36,596 KB
testcase_27 AC 67 ms
13,940 KB
testcase_28 AC 163 ms
21,904 KB
testcase_29 AC 31 ms
10,280 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):
    for t in G[s]:
        if t == p:
            continue
        if score[t] >= score[s]:
            continue
        score[t] = score[s]
        dfs(t, s)


score = list(range(sz))
for i in reversed(range(sz)):
    dfs(i, -1)

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