結果

問題 No.1194 Replace
ユーザー SPD_9X2
提出日時 2020-08-22 15:27:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 941 ms / 2,000 ms
コード長 1,037 bytes
コンパイル時間 633 ms
コンパイル使用メモリ 82,020 KB
実行使用メモリ 190,732 KB
最終ジャッジ日時 2024-10-15 09:41:44
合計ジャッジ時間 15,649 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

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