結果

問題 No.1813 Magical Stones
ユーザー MasKoaTSMasKoaTS
提出日時 2021-12-22 14:01:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 420 ms / 2,000 ms
コード長 2,280 bytes
コンパイル時間 280 ms
コンパイル使用メモリ 86,960 KB
実行使用メモリ 114,624 KB
最終ジャッジ日時 2023-10-14 13:58:16
合計ジャッジ時間 13,520 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,184 KB
testcase_01 AC 62 ms
71,204 KB
testcase_02 AC 65 ms
71,224 KB
testcase_03 AC 140 ms
102,276 KB
testcase_04 AC 62 ms
71,200 KB
testcase_05 AC 61 ms
71,468 KB
testcase_06 AC 123 ms
79,024 KB
testcase_07 AC 65 ms
71,008 KB
testcase_08 AC 79 ms
76,552 KB
testcase_09 AC 84 ms
76,612 KB
testcase_10 AC 69 ms
75,248 KB
testcase_11 AC 162 ms
89,016 KB
testcase_12 AC 63 ms
71,192 KB
testcase_13 AC 62 ms
71,200 KB
testcase_14 AC 141 ms
102,440 KB
testcase_15 AC 420 ms
110,920 KB
testcase_16 AC 400 ms
110,664 KB
testcase_17 AC 354 ms
111,072 KB
testcase_18 AC 407 ms
114,624 KB
testcase_19 AC 401 ms
110,900 KB
testcase_20 AC 394 ms
110,996 KB
testcase_21 AC 393 ms
110,888 KB
testcase_22 AC 397 ms
111,088 KB
testcase_23 AC 398 ms
110,600 KB
testcase_24 AC 93 ms
78,008 KB
testcase_25 AC 144 ms
84,968 KB
testcase_26 AC 128 ms
79,568 KB
testcase_27 AC 299 ms
100,856 KB
testcase_28 AC 310 ms
100,816 KB
testcase_29 AC 331 ms
106,828 KB
testcase_30 AC 310 ms
101,244 KB
testcase_31 AC 390 ms
110,188 KB
testcase_32 AC 63 ms
71,492 KB
testcase_33 AC 62 ms
71,264 KB
testcase_34 AC 112 ms
79,112 KB
testcase_35 AC 105 ms
78,520 KB
testcase_36 AC 110 ms
78,476 KB
testcase_37 AC 134 ms
82,460 KB
testcase_38 AC 149 ms
88,196 KB
testcase_39 AC 281 ms
99,712 KB
testcase_40 AC 129 ms
79,296 KB
testcase_41 AC 97 ms
78,056 KB
testcase_42 AC 118 ms
79,616 KB
testcase_43 AC 127 ms
79,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

"""
強連結成分分解(SCC)
from : https://github.com/ryusuke920/kyopro_educational_90_python/blob/main/solve_python/021.py
ref : https://manabitimes.jp/math/1250
"""

class SCC:
	def __init__(self, n):
		self.n = n
		self.graph = [[] for _ in range(n)]
		self.rev_graph = [[] for _ in range(n)]
		self.labels = [-1] * n
		self.lb_cnt = 0
	
	#グラフに有向辺を追加(逆向きのものを含む)
	def add_edge(self, v, nxt_v):
		self.graph[v].append(nxt_v)
		self.rev_graph[nxt_v].append(v)

	#SCCグラフの構成
	def build(self):
		self.post_order = []
		self.used = [False] * self.n
		for v in range(self.n):
			if(not self.used[v]):
				self._dfs(v)
		for v in reversed(self.post_order):
			if(self.labels[v] == -1):
				self._rev_dfs(v)
				self.lb_cnt += 1

	#SCCグラフを作る上でのDFS
	def _dfs(self, v):
		stack = [v, 0]
		while(stack):
			v, idx = stack[-2:]
			if not idx and self.used[v]:
				stack.pop()
				stack.pop()
			else:
				self.used[v] = True
				if idx < len(self.graph[v]):
					stack[-1] += 1
					stack.append(self.graph[v][idx])
					stack.append(0)
				else:
					stack.pop()
					self.post_order.append(stack.pop())

	#逆向きDFS
	def _rev_dfs(self, v):
		stack = [v]
		self.labels[v] = self.lb_cnt
		while stack:
			v = stack.pop()
			for nxt_v in self.rev_graph[v]:
				if self.labels[nxt_v] != -1:
					continue
				stack.append(nxt_v)
				self.labels[nxt_v] = self.lb_cnt

	#構成し終えたSCCグラフを連結成分ごとにリストにする
	def construct(self):
		self.dag = [[] for i in range(self.lb_cnt)]
		self.groups = [[] for i in range(self.lb_cnt)]
		for v, lb in enumerate(self.labels):
			for nxt_v in self.graph[v]:
				nxt_lb = self.labels[nxt_v]
				if lb == nxt_lb:
					continue
				self.dag[lb].append(nxt_lb)
			self.groups[lb].append(v)
		return self.dag, self.groups


"""
Main Code
"""

n,m = map(int, input().split())
scc = SCC(n)
for _ in [0] * m:
	a,b = map(int, input().split())
	a -= 1;	b -= 1
	scc.add_edge(a,b)
scc.build()
dag,_ = scc.construct()

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