結果

問題 No.1194 Replace
ユーザー roarisroaris
提出日時 2020-08-22 17:44:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 715 ms / 2,000 ms
コード長 781 bytes
コンパイル時間 352 ms
コンパイル使用メモリ 82,372 KB
実行使用メモリ 217,536 KB
最終ジャッジ日時 2024-04-23 11:33:30
合計ジャッジ時間 12,761 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 482 ms
216,052 KB
testcase_01 AC 491 ms
196,396 KB
testcase_02 AC 416 ms
151,336 KB
testcase_03 AC 336 ms
136,848 KB
testcase_04 AC 499 ms
204,912 KB
testcase_05 AC 469 ms
217,536 KB
testcase_06 AC 435 ms
198,416 KB
testcase_07 AC 685 ms
196,864 KB
testcase_08 AC 715 ms
197,568 KB
testcase_09 AC 714 ms
197,080 KB
testcase_10 AC 694 ms
197,152 KB
testcase_11 AC 687 ms
197,652 KB
testcase_12 AC 677 ms
197,232 KB
testcase_13 AC 364 ms
175,608 KB
testcase_14 AC 312 ms
159,932 KB
testcase_15 AC 300 ms
157,752 KB
testcase_16 AC 346 ms
160,024 KB
testcase_17 AC 268 ms
122,528 KB
testcase_18 AC 255 ms
143,496 KB
testcase_19 AC 345 ms
169,452 KB
testcase_20 AC 38 ms
53,836 KB
testcase_21 AC 37 ms
55,188 KB
testcase_22 AC 37 ms
54,280 KB
testcase_23 AC 158 ms
105,088 KB
testcase_24 AC 108 ms
88,488 KB
testcase_25 AC 72 ms
78,756 KB
testcase_26 AC 156 ms
110,604 KB
testcase_27 AC 73 ms
79,344 KB
testcase_28 AC 108 ms
91,124 KB
testcase_29 AC 72 ms
78,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import *

def bfs(s):
    if ma[s]!=-1:
        return
    
    q = deque([s])
    ma[s] = s
    
    while q:
        v = q.popleft()
        
        for nv in G[v]:
            if ma[nv]==-1:
                ma[nv] = s
                q.append(nv)

N, M = map(int, input().split())
BC = [tuple(map(int, input().split())) for _ in range(M)]
l = []

for B, C in BC:
    l.append(B-1)
    l.append(C-1)

l = list(set(l))
l.sort()
idx = defaultdict(int)

for i in range(len(l)):
    idx[l[i]] = i

G = [[] for _ in range(len(l))]

for B, C in BC:
    G[idx[C-1]].append(idx[B-1])

ma = [-1]*len(l)

for i in range(len(l)-1, -1, -1):
    bfs(i)

ans = N*(N+1)//2

for li in l:
    ans -= li
    ans += l[ma[idx[li]]]

print(ans)
0