結果
| 問題 |
No.1194 Replace
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-12-13 09:16:14 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 843 ms / 2,000 ms |
| コード長 | 715 bytes |
| コンパイル時間 | 255 ms |
| コンパイル使用メモリ | 82,452 KB |
| 実行使用メモリ | 195,400 KB |
| 最終ジャッジ日時 | 2024-07-22 04:24:28 |
| 合計ジャッジ時間 | 13,151 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 27 |
ソースコード
import sys
from collections import deque
input = sys.stdin.buffer.readline
N, M = map(int, input().split())
BC = [tuple(map(int, input().split())) for _ in range(M)]
nodes = set()
for b, c in BC:
nodes.add(b)
nodes.add(c)
nodes = sorted(nodes)
comp = {x: i for i, x in enumerate(nodes)}
L = len(comp)
G = [[] for _ in range(L)]
for b, c in BC:
G[comp[c]].append(comp[b])
table = nodes.copy()
for i in range(L - 1, -1, -1):
x = nodes[i]
if x < table[i]:
continue
d = deque([i])
while d:
v = d.pop()
for u in G[v]:
if table[u] < x:
table[u] = x
d.append(u)
ans = N * (N + 1) // 2 - sum(nodes) + sum(table)
print(ans)