結果

問題 No.1813 Magical Stones
ユーザー MasKoaTSMasKoaTS
提出日時 2021-10-25 17:16:19
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,263 bytes
コンパイル時間 299 ms
コンパイル使用メモリ 87,156 KB
実行使用メモリ 121,716 KB
最終ジャッジ日時 2023-09-23 07:09:00
合計ジャッジ時間 13,242 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
71,344 KB
testcase_01 AC 74 ms
71,228 KB
testcase_02 AC 74 ms
71,448 KB
testcase_03 AC 147 ms
94,380 KB
testcase_04 AC 73 ms
71,352 KB
testcase_05 AC 72 ms
71,456 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 88 ms
76,428 KB
testcase_09 WA -
testcase_10 AC 80 ms
75,252 KB
testcase_11 AC 223 ms
86,036 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 151 ms
94,232 KB
testcase_15 AC 565 ms
119,500 KB
testcase_16 WA -
testcase_17 AC 473 ms
113,408 KB
testcase_18 AC 561 ms
120,764 KB
testcase_19 AC 552 ms
119,772 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 103 ms
77,564 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 414 ms
111,348 KB
testcase_28 AC 413 ms
101,848 KB
testcase_29 AC 450 ms
108,724 KB
testcase_30 AC 417 ms
102,312 KB
testcase_31 WA -
testcase_32 AC 74 ms
71,588 KB
testcase_33 AC 74 ms
71,192 KB
testcase_34 WA -
testcase_35 AC 122 ms
78,504 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 AC 140 ms
78,872 KB
testcase_41 AC 98 ms
76,896 KB
testcase_42 AC 122 ms
78,532 KB
testcase_43 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

"""
Strongly Connected Components
source ; https://tjkendev.github.io/procon-library/python/graph/scc.html
注) PyPy3 で提出した際の TLE を防ぐため、dfs 部分を非再帰で実装
"""

# 強連結成分分解(SCC): グラフGに対するSCCを行う
# 入力: <N>: 頂点サイズ, <G>: 順方向の有向グラフ, <RG>: 逆方向の有向グラフ
# 出力: (<ラベル数>, <各頂点のラベル番号>)
def scc(N, G, RG):
    order = []
    used = [0]*N
    group = [None]*N
    cnt = [0]*N
    def dfs(s):
        que = [s]
        used[s] = 1
        while(que):
            v = que[-1]
            while(cnt[v] < len(G[v])):
                t = G[v][cnt[v]]
                cnt[v] += 1
                if not used[t]:
                    used[t] = 1
                    que.append(t)
                    break
            else:
                order.append(que.pop())
    def rdfs(s, col):
        used[s] = 1
        que = [s]
        while(que):
            v = que.pop()
            group[v] = col
            for t in RG[v]:
                if not used[t]:
                    used[t] = 1
                    que.append(t)
    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

# 縮約後のグラフを構築
def construct(N, G, label, group):
    G0 = [set() for i in range(label)]
    GP = [[] for i in range(label)]
    for v in range(N):
        lbs = group[v]
        for w in G[v]:
            lbt = group[w]
            if lbs == lbt:
                continue
            G0[lbs].add(lbt)
        GP[lbs].append(v)
    return G0,GP


"""
Main Code
"""

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

G = [[] for _ in [0]*N]
GR = [[] for _ in [0]*N]
for _ in [0]*M:
    a,b = map(int,input().split())
    G[a-1].append(b-1)
    GR[b-1].append(a-1)

label,group = scc(N,G,GR)
DAG,_ = construct(N,G,label,group)
t = len(DAG)

if(t == 1):
	print(0)
	exit(0)

source = [True]*t
sink = [True]*t
for i in range(t):
	for j in DAG[i]:
		source[i] = sink[j] = False
#ans = max(sum(source),sum(sink))
ans = max(sum(sink),sum(sink))

print(ans)
0