結果

問題 No.1194 Replace
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2020-08-22 15:27:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 998 ms / 2,000 ms
コード長 1,037 bytes
コンパイル時間 333 ms
コンパイル使用メモリ 82,324 KB
実行使用メモリ 190,936 KB
最終ジャッジ日時 2024-04-23 09:46:51
合計ジャッジ時間 16,996 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 685 ms
144,124 KB
testcase_01 AC 726 ms
142,404 KB
testcase_02 AC 595 ms
131,060 KB
testcase_03 AC 504 ms
121,324 KB
testcase_04 AC 709 ms
142,432 KB
testcase_05 AC 678 ms
143,180 KB
testcase_06 AC 627 ms
132,244 KB
testcase_07 AC 984 ms
190,368 KB
testcase_08 AC 968 ms
190,372 KB
testcase_09 AC 969 ms
190,732 KB
testcase_10 AC 974 ms
190,820 KB
testcase_11 AC 973 ms
190,936 KB
testcase_12 AC 998 ms
190,208 KB
testcase_13 AC 460 ms
107,676 KB
testcase_14 AC 387 ms
101,724 KB
testcase_15 AC 388 ms
105,512 KB
testcase_16 AC 440 ms
107,888 KB
testcase_17 AC 355 ms
104,580 KB
testcase_18 AC 343 ms
101,532 KB
testcase_19 AC 458 ms
113,148 KB
testcase_20 AC 42 ms
53,888 KB
testcase_21 AC 41 ms
53,248 KB
testcase_22 AC 43 ms
53,760 KB
testcase_23 AC 218 ms
96,768 KB
testcase_24 AC 140 ms
83,276 KB
testcase_25 AC 88 ms
77,352 KB
testcase_26 AC 195 ms
87,680 KB
testcase_27 AC 90 ms
77,144 KB
testcase_28 AC 140 ms
81,604 KB
testcase_29 AC 83 ms
77,440 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