結果

問題 No.1194 Replace
コンテスト
ユーザー 37zigen
提出日時 2020-08-31 18:23:01
言語 Python3
(3.14.7 + numpy 2.5.2 + scipy 1.18.0 + ACL)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 919 ms / 2,000 ms
+ 779µs
コード長 490 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 58 ms
コンパイル使用メモリ 15,104 KB
実行使用メモリ 76,708 KB
最終ジャッジ日時 2026-09-25 00:27:10
合計ジャッジ時間 22,850 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys
sys.setrecursionlimit(10**6)
N,M=map(int,input().split())
g=dict()
e=[]
for i in range(M):
    b,c=map(int,input().split())
    e.append((c,b))
    if c not in g:
        g[c]=[]
    g[c].append(b)
e.sort(reverse=True)
to=dict()
ans=N*(N+1)//2

def dfs(cur,v):
    global ans
    for dst in g[cur]:
        if dst in to or dst>v:
            continue
        to[dst]=v
        ans+=v-dst
        if dst in g:
            dfs(dst,v)

for src,tmp in e:
    dfs(src,src)

print(ans)
0