結果

問題 No.1194 Replace
ユーザー 37zigen
提出日時 2020-08-31 18:23:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,230 ms / 2,000 ms
コード長 490 bytes
コンパイル時間 358 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 159,852 KB
最終ジャッジ日時 2024-11-16 20:17:39
合計ジャッジ時間 23,587 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

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