結果

問題 No.19 ステージの選択
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2023-06-14 21:06:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 111 ms / 5,000 ms
コード長 1,656 bytes
コンパイル時間 513 ms
コンパイル使用メモリ 87,084 KB
実行使用メモリ 73,204 KB
最終ジャッジ日時 2023-09-05 06:54:52
合計ジャッジ時間 4,217 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 104 ms
72,876 KB
testcase_01 AC 104 ms
72,916 KB
testcase_02 AC 108 ms
73,052 KB
testcase_03 AC 105 ms
72,864 KB
testcase_04 AC 108 ms
72,868 KB
testcase_05 AC 108 ms
72,832 KB
testcase_06 AC 105 ms
72,980 KB
testcase_07 AC 104 ms
72,840 KB
testcase_08 AC 104 ms
72,892 KB
testcase_09 AC 104 ms
73,148 KB
testcase_10 AC 104 ms
73,080 KB
testcase_11 AC 103 ms
72,992 KB
testcase_12 AC 104 ms
72,752 KB
testcase_13 AC 105 ms
72,764 KB
testcase_14 AC 111 ms
72,772 KB
testcase_15 AC 104 ms
72,840 KB
testcase_16 AC 102 ms
72,856 KB
testcase_17 AC 104 ms
72,880 KB
testcase_18 AC 104 ms
72,636 KB
testcase_19 AC 102 ms
72,772 KB
testcase_20 AC 106 ms
72,876 KB
testcase_21 AC 107 ms
73,048 KB
testcase_22 AC 103 ms
72,880 KB
testcase_23 AC 104 ms
73,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import pypyjit
pypyjit.set_param('max_unroll_recursion=-1')
from collections import *
from itertools import *
from functools import *
from heapq import *
import sys,math
input = sys.stdin.readline

N = int(input())
e = [[] for _ in range(N)]
re = [[] for _ in range(N)]
L = []
for i in range(N):
    l,s = map(int,input().split())
    L.append(2*l)
    s -= 1
    if i==s:
        continue
    e[s].append(i)
    re[i].append(s)

# 強連結成分分解(SCC): グラフGに対するSCCを行う
# 入力: <N>: 頂点サイズ, <G>: 順方向の有向グラフ, <RG>: 逆方向の有向グラフ
# 出力: (<ラベル数>, <各頂点のラベル番号>)
def scc(N, G, RG):
    order = []
    used = [0]*N
    group = [None]*N
    def dfs(s):
        used[s] = 1
        for t in G[s]:
            if not used[t]:
                dfs(t)
        order.append(s)
    def rdfs(s, col):
        group[s] = col
        used[s] = 1
        for t in RG[s]:
            if not used[t]:
                rdfs(t, col)
    for i in range(N):
        if not used[i]:
            dfs(i)
    used = [0]*N
    label = 0
    for s in reversed(order):
        if not used[s]:
            rdfs(s, label)
            label += 1
    return label, group
    
    
n,group = scc(N,e,re)
G = [set() for _ in range(n)]
for i in range(N):
    gi = group[i]
    for j in e[i]:
        gj = group[j]
        if gj==gi:
            continue
        G[gj].add(gi)
X = [[] for _ in range(n)]
for i,g in enumerate(group):
    X[g].append(L[i])

ans = 0

for i in range(n):

    if len(G[i])==0:
        ans += sum(X[i])//2 + min(X[i])//2
    else:
        ans += sum(X[i])//2
print(ans*0.5)
0