結果

問題 No.1813 Magical Stones
ユーザー MasKoaTSMasKoaTS
提出日時 2021-10-24 13:40:47
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,781 bytes
コンパイル時間 2,042 ms
コンパイル使用メモリ 86,800 KB
実行使用メモリ 837,356 KB
最終ジャッジ日時 2023-09-23 07:04:40
合計ジャッジ時間 15,410 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,388 KB
testcase_01 AC 74 ms
71,516 KB
testcase_02 AC 73 ms
71,200 KB
testcase_03 AC 130 ms
98,816 KB
testcase_04 AC 78 ms
71,288 KB
testcase_05 AC 76 ms
71,308 KB
testcase_06 AC 163 ms
79,244 KB
testcase_07 AC 76 ms
71,336 KB
testcase_08 AC 90 ms
76,432 KB
testcase_09 AC 99 ms
76,520 KB
testcase_10 AC 81 ms
75,372 KB
testcase_11 AC 287 ms
88,972 KB
testcase_12 AC 77 ms
71,208 KB
testcase_13 AC 77 ms
71,488 KB
testcase_14 AC 131 ms
98,948 KB
testcase_15 AC 980 ms
160,724 KB
testcase_16 AC 1,026 ms
162,636 KB
testcase_17 AC 935 ms
174,516 KB
testcase_18 AC 982 ms
161,196 KB
testcase_19 AC 969 ms
161,300 KB
testcase_20 AC 1,050 ms
170,872 KB
testcase_21 AC 977 ms
160,164 KB
testcase_22 MLE -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**6)

"""
Strongly Connected Components
source ; https://tjkendev.github.io/procon-library/python/graph/scc.html
"""

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

print(ans)
0