結果

問題 No.1813 Magical Stones
ユーザー titiatitia
提出日時 2022-01-25 02:57:23
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,631 bytes
コンパイル時間 371 ms
コンパイル使用メモリ 87,112 KB
実行使用メモリ 117,512 KB
最終ジャッジ日時 2023-08-21 18:21:26
合計ジャッジ時間 22,038 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,368 KB
testcase_01 AC 72 ms
71,236 KB
testcase_02 AC 73 ms
71,464 KB
testcase_03 AC 166 ms
99,868 KB
testcase_04 AC 73 ms
71,392 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 153 ms
99,840 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 900 ms
115,328 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 73 ms
71,060 KB
testcase_33 AC 73 ms
71,404 KB
testcase_34 WA -
testcase_35 AC 212 ms
81,064 KB
testcase_36 WA -
testcase_37 AC 354 ms
85,692 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 153 ms
79,340 KB
testcase_42 AC 228 ms
81,076 KB
testcase_43 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M=map(int,input().split())

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

# (一般グラフの)トポロジカルソート、SCC

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

for i in range(M):
    x,y=map(int,input().split())
    x-=1
    y-=1
    E[x].append(y)
    E_INV[y].append(x)
    Union(x,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))

ANS=-1

for i in range(N):
    if find(i)==i:
        ANS+=1

if ANS!=0:
    ANS+=1

USE=[0]*N

for i in range(len(SCC)-1,-1,-1):
    for x in SCC[i]:
        if USE[x]==0:
            if len(SCC[i])==Nodes[find(x)]:
                True
            else:
                ANS+=1
            Q=[x]
            USE[x]=1

            while Q:
                x=Q.pop()

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