結果
| 問題 | 
                            No.1813 Magical Stones
                             | 
                    
| コンテスト | |
| ユーザー | 
                             MasKoaTS
                         | 
                    
| 提出日時 | 2021-10-24 13:45:13 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 1,011 ms / 2,000 ms | 
| コード長 | 1,777 bytes | 
| コンパイル時間 | 345 ms | 
| コンパイル使用メモリ | 81,920 KB | 
| 実行使用メモリ | 211,968 KB | 
| 最終ジャッジ日時 | 2024-07-16 06:57:22 | 
| 合計ジャッジ時間 | 16,101 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 40 | 
ソースコード
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
"""
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)
            
            
            
        
            
MasKoaTS