結果

問題 No.1194 Replace
ユーザー gew1fw
提出日時 2025-06-12 20:38:34
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 963 bytes
コンパイル時間 154 ms
コンパイル使用メモリ 81,644 KB
実行使用メモリ 169,800 KB
最終ジャッジ日時 2025-06-12 20:38:44
合計ジャッジ時間 8,902 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other WA * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import defaultdict

def main():
    N, M = map(int, sys.stdin.readline().split())
    operations = []
    for _ in range(M):
        B, C = map(int, sys.stdin.readline().split())
        if C > B:
            operations.append((B, C))
    
    S = set()
    edges = defaultdict(list)
    for B, C in operations:
        edges[B].append(C)
        S.add(B)
        S.add(C)
    
    sorted_S = sorted(S, reverse=True)
    max_reach = {x: x for x in S}
    
    for x in sorted_S:
        if x in edges:
            max_val = 0
            for y in edges[x]:
                if max_reach[y] > max_val:
                    max_val = max_reach[y]
            max_reach[x] = max(x, max_val)
    
    sum_S_contribution = sum(max_reach[x] for x in S)
    sum_in_S = sum(S)
    sum_total = N * (N + 1) // 2
    sum_out_S = sum_total - sum_in_S
    total = sum_S_contribution + sum_out_S
    print(total)

if __name__ == "__main__":
    main()
0