結果

問題 No.19 ステージの選択
ユーザー 👑 rin204rin204
提出日時 2022-07-10 18:09:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 74 ms / 5,000 ms
コード長 2,058 bytes
コンパイル時間 288 ms
コンパイル使用メモリ 87,220 KB
実行使用メモリ 71,688 KB
最終ジャッジ日時 2023-09-02 01:30:44
合計ジャッジ時間 3,543 ms
ジャッジサーバーID
(参考情報)
judge12 / judge16
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,200 KB
testcase_01 AC 71 ms
71,436 KB
testcase_02 AC 71 ms
71,452 KB
testcase_03 AC 72 ms
71,496 KB
testcase_04 AC 73 ms
71,308 KB
testcase_05 AC 74 ms
71,356 KB
testcase_06 AC 72 ms
71,688 KB
testcase_07 AC 73 ms
71,324 KB
testcase_08 AC 74 ms
71,136 KB
testcase_09 AC 73 ms
71,452 KB
testcase_10 AC 71 ms
71,572 KB
testcase_11 AC 70 ms
71,236 KB
testcase_12 AC 69 ms
71,384 KB
testcase_13 AC 70 ms
71,504 KB
testcase_14 AC 71 ms
71,128 KB
testcase_15 AC 71 ms
71,448 KB
testcase_16 AC 72 ms
71,392 KB
testcase_17 AC 72 ms
71,544 KB
testcase_18 AC 71 ms
71,296 KB
testcase_19 AC 73 ms
71,476 KB
testcase_20 AC 72 ms
71,252 KB
testcase_21 AC 73 ms
71,352 KB
testcase_22 AC 73 ms
71,380 KB
testcase_23 AC 73 ms
71,524 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def scc(N, G):
    order = []
    used = [False]*N
    group = [None]*N
    RG = [[] for _ in range(N)]
    for i in range(N):
        for j in G[i]:
            RG[j].append(i)
    
    def dfs(pos):
        stack = [(1, pos), (0, pos)]
        while stack:
            t, pos = stack.pop()
            if t == 0:
                if used[pos]:
                    stack.pop()
                    continue
                used[pos] = True
                for npos in G[pos]:
                    if not used[npos]:
                        stack.append((1, npos))
                        stack.append((0, npos))
            else:
                order.append(pos)

    def rdfs(pos, col):
        stack = [pos]
        group[pos] = col
        used[pos] = True
        while stack:
            pos = stack.pop()
            for npos in RG[pos]:
                if not used[npos]:
                    used[npos] = True
                    group[npos] = col
                    stack.append(npos)
                    
    for i in range(N):
        if not used[i]:
            dfs(i)
    used = [False]*N
    label = 0
    for s in reversed(order):
        if not used[s]:
            rdfs(s, label)
            label += 1
    return label, group

def construct(N, G, label, group):
    G0 = [[] for _ in range(label)]
    GP = [[] for _ in range(label)]
    for v in range(N):
        lbs = group[v]
        for w in G[v]:
            lbt = group[w]
            if lbs == lbt:
                continue
            G0[lbs].append(lbt)
        GP[lbs].append(v)
    return G0, GP

n = int(input())
edges = [[] for _ in range(n)]
L = []
for i in range(n):
    l, s = map(int, input().split())
    s -= 1
    L.append(l)
    edges[s].append(i)

label, group = scc(n, edges)
g0, gp = construct(n, edges, label, group)
in_ = [0] * label

for i in range(label):
    for j in g0[i]:
        in_[j] += 1
ans = sum(L)
for i in range(label):
    if in_[i] == 0:
        mi = 1 << 30
        for p in gp[i]:
            mi = min(mi, L[p])
        ans += mi
print(ans / 2)

0