結果

問題 No.19 ステージの選択
ユーザー convexineqconvexineq
提出日時 2021-02-18 17:54:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 78 ms / 5,000 ms
コード長 1,814 bytes
コンパイル時間 925 ms
コンパイル使用メモリ 86,692 KB
実行使用メモリ 71,452 KB
最終ジャッジ日時 2023-10-13 01:42:16
合計ジャッジ時間 3,654 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,140 KB
testcase_01 AC 74 ms
71,200 KB
testcase_02 AC 75 ms
71,384 KB
testcase_03 AC 72 ms
71,136 KB
testcase_04 AC 75 ms
70,904 KB
testcase_05 AC 75 ms
71,400 KB
testcase_06 AC 74 ms
71,416 KB
testcase_07 AC 74 ms
71,392 KB
testcase_08 AC 77 ms
71,416 KB
testcase_09 AC 75 ms
71,248 KB
testcase_10 AC 75 ms
71,448 KB
testcase_11 AC 73 ms
71,132 KB
testcase_12 AC 73 ms
71,124 KB
testcase_13 AC 73 ms
71,240 KB
testcase_14 AC 73 ms
70,952 KB
testcase_15 AC 73 ms
70,924 KB
testcase_16 AC 74 ms
71,284 KB
testcase_17 AC 73 ms
71,380 KB
testcase_18 AC 78 ms
70,916 KB
testcase_19 AC 73 ms
71,304 KB
testcase_20 AC 73 ms
71,140 KB
testcase_21 AC 77 ms
71,444 KB
testcase_22 AC 74 ms
71,400 KB
testcase_23 AC 73 ms
71,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
0