結果
| 問題 | No.19 ステージの選択 |
| コンテスト | |
| ユーザー |
convexineq
|
| 提出日時 | 2021-02-18 17:54:57 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 42 ms / 5,000 ms |
| コード長 | 1,814 bytes |
| コンパイル時間 | 274 ms |
| コンパイル使用メモリ | 81,720 KB |
| 実行使用メモリ | 54,416 KB |
| 最終ジャッジ日時 | 2024-09-14 23:28:18 |
| 合計ジャッジ時間 | 2,090 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 24 |
ソースコード
def SCC_Tarjan(g):
n = len(g)
order = [-1]*n # 負なら未処理、[0,n) ならpre-order, n ならvisited
low = [0]*n
ord_now = 0
parent = [-1]*n
gp = [0]*n
gp_num = 0
S = []
q = []
for i in range(n):
if order[i] == -1:
q.append(i)
while q:
v = q.pop()
if v >= 0:
if order[v] != -1: continue
order[v] = low[v] = ord_now
ord_now += 1
S.append(v)
q.append(~v)
for c in g[v]:
if order[c] == -1:
q.append(c)
parent[c] = v
else:
low[v] = min(low[v], order[c])
else:
v = ~v
if parent[v] != -1:
low[parent[v]] = min(low[parent[v]], low[v])
if low[v] == order[v]:
while True:
w = S.pop()
order[w] = n
gp[w] = gp_num
if w==v: break
gp_num += 1
scc = [[] for _ in range(gp_num)]
for i in range(n):
gp[i] = gp_num-gp[i]-1
scc[gp[i]].append(i)
return scc, gp, gp_num
n = int(input())
g = [[] for _ in range(n)]
diff = [0]*n
for i in range(n):
l,s = map(int,input().split())
g[s-1].append(i)
diff[i] = l
scc,gp,m = SCC_Tarjan(g)
indegree = [0]*m
for v in range(n):
for c in g[v]:
if gp[v] != gp[c]:
indegree[gp[c]] = 1
ans = sum(diff)
for i in range(m):
if indegree[i] == 0:
ans += min(diff[k] for k in scc[i])
print(ans/2)
convexineq