結果

問題 No.1813 Magical Stones
ユーザー MasKoaTSMasKoaTS
提出日時 2021-10-24 13:32:57
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 1,740 bytes
コンパイル時間 985 ms
コンパイル使用メモリ 10,884 KB
実行使用メモリ 64,072 KB
最終ジャッジ日時 2023-09-23 07:03:20
合計ジャッジ時間 18,555 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,356 KB
testcase_01 AC 16 ms
8,492 KB
testcase_02 AC 17 ms
8,476 KB
testcase_03 AC 429 ms
64,024 KB
testcase_04 AC 16 ms
8,312 KB
testcase_05 AC 17 ms
8,424 KB
testcase_06 AC 35 ms
11,020 KB
testcase_07 AC 19 ms
8,160 KB
testcase_08 AC 20 ms
8,904 KB
testcase_09 AC 22 ms
9,248 KB
testcase_10 AC 20 ms
8,704 KB
testcase_11 AC 260 ms
26,348 KB
testcase_12 AC 17 ms
8,460 KB
testcase_13 AC 18 ms
8,452 KB
testcase_14 AC 434 ms
64,072 KB
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 AC 26 ms
8,940 KB
testcase_25 AC 147 ms
19,512 KB
testcase_26 AC 53 ms
12,100 KB
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 AC 17 ms
8,300 KB
testcase_33 AC 17 ms
8,484 KB
testcase_34 RE -
testcase_35 RE -
testcase_36 AC 36 ms
10,776 KB
testcase_37 RE -
testcase_38 AC 330 ms
44,452 KB
testcase_39 RE -
testcase_40 AC 41 ms
10,652 KB
testcase_41 AC 50 ms
9,260 KB
testcase_42 RE -
testcase_43 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

"""
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