結果

問題 No.1813 Magical Stones
ユーザー titiatitia
提出日時 2022-01-25 03:15:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 682 ms / 2,000 ms
コード長 2,549 bytes
コンパイル時間 1,257 ms
コンパイル使用メモリ 87,024 KB
実行使用メモリ 112,272 KB
最終ジャッジ日時 2023-08-21 18:25:28
合計ジャッジ時間 15,734 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,312 KB
testcase_01 AC 77 ms
71,204 KB
testcase_02 AC 79 ms
71,036 KB
testcase_03 AC 152 ms
100,648 KB
testcase_04 AC 76 ms
71,004 KB
testcase_05 AC 75 ms
71,168 KB
testcase_06 AC 154 ms
79,548 KB
testcase_07 AC 76 ms
71,316 KB
testcase_08 AC 90 ms
76,480 KB
testcase_09 AC 101 ms
76,200 KB
testcase_10 AC 86 ms
75,232 KB
testcase_11 AC 241 ms
88,496 KB
testcase_12 AC 79 ms
71,168 KB
testcase_13 AC 76 ms
71,112 KB
testcase_14 AC 156 ms
100,712 KB
testcase_15 AC 681 ms
110,952 KB
testcase_16 AC 681 ms
110,824 KB
testcase_17 AC 582 ms
111,184 KB
testcase_18 AC 678 ms
111,360 KB
testcase_19 AC 682 ms
111,076 KB
testcase_20 AC 673 ms
110,768 KB
testcase_21 AC 660 ms
111,620 KB
testcase_22 AC 678 ms
111,000 KB
testcase_23 AC 663 ms
110,856 KB
testcase_24 AC 135 ms
77,836 KB
testcase_25 AC 209 ms
84,140 KB
testcase_26 AC 177 ms
79,688 KB
testcase_27 AC 496 ms
101,136 KB
testcase_28 AC 586 ms
101,684 KB
testcase_29 AC 590 ms
102,732 KB
testcase_30 AC 548 ms
102,940 KB
testcase_31 AC 647 ms
112,272 KB
testcase_32 AC 76 ms
71,132 KB
testcase_33 AC 77 ms
71,244 KB
testcase_34 AC 169 ms
78,984 KB
testcase_35 AC 159 ms
79,736 KB
testcase_36 AC 150 ms
79,248 KB
testcase_37 AC 240 ms
82,644 KB
testcase_38 AC 209 ms
93,844 KB
testcase_39 AC 472 ms
104,764 KB
testcase_40 AC 163 ms
79,232 KB
testcase_41 AC 125 ms
77,880 KB
testcase_42 AC 159 ms
79,588 KB
testcase_43 AC 203 ms
80,940 KB
権限があれば一括ダウンロードができます

ソースコード

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)

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

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

if Nodes[find(0)]==N:
    print(0)
    exit()

IN=[0]*N
OUT=[0]*N

for i in range(N):
    for to in E[i]:
        if find(i)==find(to):
            continue
        OUT[find(i)]+=1
        IN[find(to)]+=1

ANS=0
ANS2=0

for i in range(N):
    if find(i)==i:
        if OUT[i]==0:
            ANS+=1
        if IN[i]==0:
            ANS2+=1

print(max(ANS,ANS2))
0