結果

問題 No.1813 Magical Stones
ユーザー MasKoaTSMasKoaTS
提出日時 2021-12-13 12:19:57
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 948 ms / 2,000 ms
コード長 1,799 bytes
コンパイル時間 515 ms
コンパイル使用メモリ 10,844 KB
実行使用メモリ 64,160 KB
最終ジャッジ日時 2023-09-29 14:54:28
合計ジャッジ時間 17,002 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,080 KB
testcase_01 AC 16 ms
8,096 KB
testcase_02 AC 17 ms
8,420 KB
testcase_03 AC 400 ms
64,032 KB
testcase_04 AC 16 ms
8,116 KB
testcase_05 AC 16 ms
8,092 KB
testcase_06 AC 31 ms
10,940 KB
testcase_07 AC 17 ms
8,196 KB
testcase_08 AC 19 ms
8,900 KB
testcase_09 AC 22 ms
9,344 KB
testcase_10 AC 19 ms
8,780 KB
testcase_11 AC 187 ms
26,212 KB
testcase_12 AC 17 ms
8,220 KB
testcase_13 AC 16 ms
8,088 KB
testcase_14 AC 375 ms
64,160 KB
testcase_15 AC 906 ms
59,904 KB
testcase_16 AC 948 ms
59,940 KB
testcase_17 AC 839 ms
55,236 KB
testcase_18 AC 910 ms
59,824 KB
testcase_19 AC 912 ms
59,820 KB
testcase_20 AC 932 ms
59,660 KB
testcase_21 AC 947 ms
59,940 KB
testcase_22 AC 948 ms
59,856 KB
testcase_23 AC 946 ms
59,924 KB
testcase_24 AC 22 ms
8,924 KB
testcase_25 AC 109 ms
19,628 KB
testcase_26 AC 43 ms
12,140 KB
testcase_27 AC 619 ms
48,256 KB
testcase_28 AC 689 ms
41,936 KB
testcase_29 AC 702 ms
50,108 KB
testcase_30 AC 672 ms
46,244 KB
testcase_31 AC 874 ms
58,556 KB
testcase_32 AC 16 ms
8,328 KB
testcase_33 AC 16 ms
8,344 KB
testcase_34 AC 34 ms
9,812 KB
testcase_35 AC 80 ms
12,224 KB
testcase_36 AC 31 ms
10,816 KB
testcase_37 AC 169 ms
17,520 KB
testcase_38 AC 259 ms
44,400 KB
testcase_39 AC 627 ms
51,772 KB
testcase_40 AC 32 ms
10,644 KB
testcase_41 AC 33 ms
9,288 KB
testcase_42 AC 83 ms
12,212 KB
testcase_43 AC 58 ms
11,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline

"""
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 _ 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]:
		sink[i] = source[j] = False
ans = max(sum(source),sum(sink))

print(ans)
0