結果

問題 No.1995 CHIKA Road
ユーザー LyricalMaestro
提出日時 2025-09-13 10:14:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 353 ms / 2,000 ms
コード長 848 bytes
コンパイル時間 514 ms
コンパイル使用メモリ 82,716 KB
実行使用メモリ 163,616 KB
最終ジャッジ日時 2025-09-13 10:15:06
合計ジャッジ時間 9,162 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

## https://yukicoder.me/problems/no/1995

import math

def main():
    N, M = map(int, input().split())
    ab = []
    b_map = {}
    for _ in range(M):
        a, b = map(int ,input().split())
        ab.append((a ,b))
        if b not in b_map:
            b_map[b] = []
        b_map[b].append(a)
    
    pos_set = set()
    for a, b in ab:
        pos_set.add(a)
        pos_set.add(b)
    pos_set.add(1)
    pos_set.add(N)
    pos_list = list(pos_set)
    pos_list.sort()

    answer = 0
    dp = {1: 0}
    for i in range(len(pos_list)):
        p = pos_list[i]

        ans = answer
        if p in b_map:
            for q in b_map[p]:
                ans = max(ans, dp[q] + 1)
        dp[p] = ans
        answer = ans
    
    x = dp[N]
    answer = 2 * (N - 1) - x
    print(answer)








    



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