結果

問題 No.1194 Replace
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2020-08-22 15:27:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 994 ms / 2,000 ms
コード長 1,037 bytes
コンパイル時間 317 ms
コンパイル使用メモリ 87,192 KB
実行使用メモリ 189,964 KB
最終ジャッジ日時 2023-08-05 12:36:13
合計ジャッジ時間 17,537 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 665 ms
144,960 KB
testcase_01 AC 693 ms
143,400 KB
testcase_02 AC 572 ms
132,360 KB
testcase_03 AC 507 ms
124,816 KB
testcase_04 AC 688 ms
143,448 KB
testcase_05 AC 674 ms
144,676 KB
testcase_06 AC 612 ms
133,184 KB
testcase_07 AC 950 ms
189,820 KB
testcase_08 AC 965 ms
189,732 KB
testcase_09 AC 994 ms
189,964 KB
testcase_10 AC 989 ms
189,504 KB
testcase_11 AC 959 ms
189,656 KB
testcase_12 AC 958 ms
189,848 KB
testcase_13 AC 442 ms
113,092 KB
testcase_14 AC 395 ms
103,948 KB
testcase_15 AC 396 ms
107,816 KB
testcase_16 AC 432 ms
108,172 KB
testcase_17 AC 368 ms
104,828 KB
testcase_18 AC 342 ms
101,340 KB
testcase_19 AC 452 ms
114,080 KB
testcase_20 AC 87 ms
71,656 KB
testcase_21 AC 87 ms
71,720 KB
testcase_22 AC 86 ms
71,360 KB
testcase_23 AC 229 ms
98,072 KB
testcase_24 AC 169 ms
84,516 KB
testcase_25 AC 123 ms
78,740 KB
testcase_26 AC 214 ms
89,960 KB
testcase_27 AC 126 ms
78,400 KB
testcase_28 AC 173 ms
83,160 KB
testcase_29 AC 120 ms
78,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""

https://yukicoder.me/problems/no/1194

できるだけ大きい数字にしたい
グラフを構築する
目的の数字になったら操作しなければいいだけ
各点から行ける最大のノードを探索すればいい

"""

from sys import stdin
import sys

N,M = map(int,stdin.readline().split())

lis = {}
nums = []
maxi = {}

for i in range(M):

    b,c = map(int,stdin.readline().split())
    if b not in lis:
        lis[b] = []
        nums.append(b)
        maxi[b] = b
    if c not in lis:
        lis[c] = []
        nums.append(c)
        maxi[c] = c
    lis[c].append(b)

nums.sort()
nums.reverse()

from collections import deque

for s in nums:

    if maxi[s] > s:
        continue
    
    q = deque([s])
    while len(q) > 0:
        now = q.popleft()
        for nex in lis[now]:
            if maxi[nex] < maxi[now]:
                maxi[nex] = maxi[now]
                q.append(nex)

ans = (1+N) * N // 2
for i in maxi:

    if i <= N:
        ans -= i
        ans += maxi[i]

print (ans)






0