結果

問題 No.1813 Magical Stones
ユーザー MasKoaTSMasKoaTS
提出日時 2021-10-24 21:26:09
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,083 ms / 2,000 ms
コード長 1,799 bytes
コンパイル時間 91 ms
コンパイル使用メモリ 10,952 KB
実行使用メモリ 64,092 KB
最終ジャッジ日時 2023-09-23 07:07:35
合計ジャッジ時間 18,993 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,232 KB
testcase_01 AC 17 ms
8,120 KB
testcase_02 AC 17 ms
8,348 KB
testcase_03 AC 475 ms
64,040 KB
testcase_04 AC 17 ms
8,120 KB
testcase_05 AC 17 ms
8,088 KB
testcase_06 AC 33 ms
10,928 KB
testcase_07 AC 18 ms
8,056 KB
testcase_08 AC 21 ms
9,040 KB
testcase_09 AC 22 ms
9,452 KB
testcase_10 AC 20 ms
8,772 KB
testcase_11 AC 233 ms
26,204 KB
testcase_12 AC 17 ms
8,100 KB
testcase_13 AC 17 ms
8,040 KB
testcase_14 AC 463 ms
64,092 KB
testcase_15 AC 1,082 ms
60,008 KB
testcase_16 AC 1,083 ms
59,880 KB
testcase_17 AC 1,000 ms
55,264 KB
testcase_18 AC 1,083 ms
59,960 KB
testcase_19 AC 1,074 ms
60,024 KB
testcase_20 AC 1,079 ms
59,732 KB
testcase_21 AC 1,053 ms
60,008 KB
testcase_22 AC 1,062 ms
59,740 KB
testcase_23 AC 1,029 ms
59,832 KB
testcase_24 AC 22 ms
9,044 KB
testcase_25 AC 124 ms
19,712 KB
testcase_26 AC 45 ms
12,048 KB
testcase_27 AC 681 ms
48,188 KB
testcase_28 AC 765 ms
42,104 KB
testcase_29 AC 782 ms
50,176 KB
testcase_30 AC 721 ms
46,140 KB
testcase_31 AC 956 ms
58,576 KB
testcase_32 AC 17 ms
8,336 KB
testcase_33 AC 16 ms
8,300 KB
testcase_34 AC 35 ms
9,800 KB
testcase_35 AC 80 ms
12,148 KB
testcase_36 AC 32 ms
10,908 KB
testcase_37 AC 186 ms
17,768 KB
testcase_38 AC 289 ms
44,420 KB
testcase_39 AC 696 ms
51,760 KB
testcase_40 AC 33 ms
10,580 KB
testcase_41 AC 34 ms
9,480 KB
testcase_42 AC 85 ms
12,248 KB
testcase_43 AC 58 ms
11,720 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]:
		source[i] = sink[j] = False
ans = max(sum(source),sum(sink))

print(ans)
0