結果

問題 No.19 ステージの選択
ユーザー titiatitia
提出日時 2021-11-09 01:03:17
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 17 ms / 5,000 ms
コード長 2,505 bytes
コンパイル時間 908 ms
コンパイル使用メモリ 11,164 KB
実行使用メモリ 8,544 KB
最終ジャッジ日時 2023-08-10 20:04:58
合計ジャッジ時間 2,552 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,416 KB
testcase_01 AC 16 ms
8,312 KB
testcase_02 AC 16 ms
8,484 KB
testcase_03 AC 16 ms
8,404 KB
testcase_04 AC 16 ms
8,320 KB
testcase_05 AC 16 ms
8,372 KB
testcase_06 AC 16 ms
8,456 KB
testcase_07 AC 16 ms
8,544 KB
testcase_08 AC 17 ms
8,332 KB
testcase_09 AC 15 ms
8,336 KB
testcase_10 AC 17 ms
8,404 KB
testcase_11 AC 16 ms
8,484 KB
testcase_12 AC 16 ms
8,484 KB
testcase_13 AC 15 ms
8,464 KB
testcase_14 AC 15 ms
8,440 KB
testcase_15 AC 16 ms
8,448 KB
testcase_16 AC 16 ms
8,536 KB
testcase_17 AC 16 ms
8,440 KB
testcase_18 AC 17 ms
8,384 KB
testcase_19 AC 16 ms
8,296 KB
testcase_20 AC 16 ms
8,292 KB
testcase_21 AC 17 ms
8,432 KB
testcase_22 AC 17 ms
8,316 KB
testcase_23 AC 17 ms
8,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N=int(input())
LS=[list(map(int,input().split())) for i in range(N)]

for i in range(N):
    LS[i][1]-=1

E=[[] for i in range(N)]
E_INV=[[] for i in range(N)]

for i in range(N):
    x,y=LS[i]
    E[y].append(i)
    E_INV[i].append(y)

# DFSして帰り際にTOPに点を放り込んでいる。
# NOWで現在地点、USEINDで、どこの辺まで既に見たか、を調べている。
def Top_sort(E):
    Parent=[-1]*N
    USEIND=[0]*N
    TOP=[]

    for ROOT in range(N):
        if Parent[ROOT]!=-1:
            continue
        Parent[ROOT]=ROOT

        NOW=ROOT

        while NOW!=ROOT or USEIND[ROOT]!=len(E[ROOT]):

            if USEIND[NOW]==len(E[NOW]):
                TOP.append(NOW)
                NOW=Parent[NOW]
            elif E[NOW][USEIND[NOW]]==Parent[NOW]:
                USEIND[NOW]+=1
            else:
                NEXT=E[NOW][USEIND[NOW]]
                USEIND[NOW]+=1
                if Parent[NEXT]==-1:
                    Parent[NEXT]=NOW
                    NOW=NEXT
        TOP.append(ROOT)
        
    return TOP[::-1]


USE=[0]*N
SCC=[]

# SCCを調べるための逆順DFS。
# やっていることはhttps://manabitimes.jp/math/1250 などと同じ。
def dfs2(x):
    Q=[x]
    USE[x]=1
    ANS=[]

    while Q:
        x=Q.pop()
        ANS.append(x)
        for to in E_INV[x]:
            if USE[to]==0:
                USE[to]=1
                Q.append(to)
    return ANS

TOP_SORT=Top_sort(E)

for x in TOP_SORT:
    if USE[x]==0:
        SCC.append(dfs2(x))

# UnionFind

Group = [i for i in range(N+1)] # グループ分け
Nodes = [1]*(N+1) # 各グループのノードの数

def find(x):
    while Group[x] != x:
        x=Group[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        if Nodes[find(x)] < Nodes[find(y)]:
            
            Nodes[find(y)] += Nodes[find(x)]
            Nodes[find(x)] = 0
            Group[find(x)] = find(y)
            
        else:
            Nodes[find(x)] += Nodes[find(y)]
            Nodes[find(y)] = 0
            Group[find(y)] = find(x)

for scc in SCC:
    for i in range(1,len(scc)):
        Union(scc[i],scc[i-1])

USE=[0]*N
ANS=0

for scc in SCC:
    x=find(scc[0])
    flag=0

    for n in scc:
        for fr in E_INV[n]:
            if USE[fr]==1:
                flag=1

    X=[]
    
    for n in scc:
        X.append(LS[n][0])
            
    if flag==0:
        ANS+=sum(X)/2+min(X)/2
    else:
        ANS+=sum(X)/2

    for n in scc:
        USE[n]=1
print(ANS)
0