結果

問題 No.1194 Replace
ユーザー tamato
提出日時 2020-08-27 16:45:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 467 ms / 2,000 ms
コード長 956 bytes
コンパイル時間 469 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 158,044 KB
最終ジャッジ日時 2024-11-07 20:40:47
合計ジャッジ時間 12,770 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    from collections import deque
    input = sys.stdin.readline

    N, M = map(int, input().split())
    adj = {}
    for _ in range(M):
        b, c = map(int, input().split())
        if c not in adj:
            adj[c] = {b}
        else:
            adj[c].add(b)

    ans = N * (N+1) // 2
    C = sorted(list(adj.keys()), reverse=True)
    seen = set()
    for c in C:
        if c in seen:
            continue
        que = deque()
        que.append(c)
        B = set()
        while que:
            v = que.popleft()
            if v not in adj:
                continue
            for u in adj[v]:
                if u > c:
                    continue
                if u not in seen:
                    seen.add(u)
                    que.append(u)
                    B.add(u)
        for b in B:
            ans += c - b
    print(ans)


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