結果

問題 No.1813 Magical Stones
ユーザー titiatitia
提出日時 2022-01-25 03:09:11
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,500 bytes
コンパイル時間 601 ms
コンパイル使用メモリ 87,276 KB
実行使用メモリ 112,056 KB
最終ジャッジ日時 2023-08-21 18:23:43
合計ジャッジ時間 15,597 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,184 KB
testcase_01 AC 72 ms
71,296 KB
testcase_02 WA -
testcase_03 AC 150 ms
100,800 KB
testcase_04 AC 73 ms
71,372 KB
testcase_05 WA -
testcase_06 AC 142 ms
79,428 KB
testcase_07 AC 76 ms
71,320 KB
testcase_08 AC 88 ms
76,408 KB
testcase_09 AC 95 ms
76,560 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 73 ms
71,424 KB
testcase_13 AC 72 ms
71,160 KB
testcase_14 AC 150 ms
100,492 KB
testcase_15 WA -
testcase_16 AC 646 ms
110,908 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 659 ms
110,824 KB
testcase_21 AC 650 ms
111,380 KB
testcase_22 AC 660 ms
111,256 KB
testcase_23 AC 660 ms
110,736 KB
testcase_24 WA -
testcase_25 AC 188 ms
84,108 KB
testcase_26 AC 165 ms
79,804 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 629 ms
112,056 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 162 ms
79,088 KB
testcase_35 WA -
testcase_36 AC 140 ms
79,232 KB
testcase_37 AC 226 ms
82,796 KB
testcase_38 AC 199 ms
93,856 KB
testcase_39 AC 451 ms
104,800 KB
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 198 ms
80,944 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])

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(i)]+=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