結果

問題 No.1194 Replace
ユーザー titia
提出日時 2020-08-22 16:55:34
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 985 ms / 2,000 ms
コード長 679 bytes
コンパイル時間 135 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 65,780 KB
最終ジャッジ日時 2024-10-15 10:57:42
合計ジャッジ時間 18,213 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from operator import itemgetter

N,M=map(int,input().split())
E=dict()

for i in range(M):
    x,y=map(int,input().split())
    if y in E:
        E[y].append(x)
    else:
        E[y]=[x]

USESET=set()
SCORE=N*(N+1)//2

KEYS=sorted(E.keys(),reverse=True)

for i in KEYS:
    if i in USESET:
        continue

    Q=[i]
    while Q:
        x=Q.pop()
        if not (x in E):
            continue
        for to in E[x]:
            if to in USESET:
                continue
            else:
                USESET.add(to)
                if i>to:
                    SCORE+=i-to
                Q.append(to)

print(SCORE)
                
0