結果

問題 No.1813 Magical Stones
ユーザー MasKoaTSMasKoaTS
提出日時 2021-09-23 11:52:42
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,491 ms / 2,000 ms
コード長 1,986 bytes
コンパイル時間 338 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 59,648 KB
最終ジャッジ日時 2024-07-16 06:56:29
合計ジャッジ時間 24,512 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
11,008 KB
testcase_01 AC 28 ms
11,136 KB
testcase_02 AC 29 ms
11,136 KB
testcase_03 AC 368 ms
55,680 KB
testcase_04 AC 29 ms
11,008 KB
testcase_05 AC 29 ms
11,136 KB
testcase_06 AC 54 ms
13,184 KB
testcase_07 AC 29 ms
11,008 KB
testcase_08 AC 32 ms
11,392 KB
testcase_09 AC 35 ms
11,904 KB
testcase_10 AC 31 ms
11,392 KB
testcase_11 AC 254 ms
26,112 KB
testcase_12 AC 29 ms
11,008 KB
testcase_13 AC 28 ms
11,008 KB
testcase_14 AC 365 ms
55,680 KB
testcase_15 AC 1,466 ms
59,644 KB
testcase_16 AC 1,419 ms
59,644 KB
testcase_17 AC 1,268 ms
50,452 KB
testcase_18 AC 1,450 ms
59,648 KB
testcase_19 AC 1,432 ms
59,516 KB
testcase_20 AC 1,392 ms
59,520 KB
testcase_21 AC 1,443 ms
59,648 KB
testcase_22 AC 1,383 ms
59,348 KB
testcase_23 AC 1,491 ms
59,520 KB
testcase_24 AC 41 ms
11,392 KB
testcase_25 AC 172 ms
20,608 KB
testcase_26 AC 73 ms
14,208 KB
testcase_27 AC 951 ms
47,744 KB
testcase_28 AC 1,200 ms
41,452 KB
testcase_29 AC 1,172 ms
49,152 KB
testcase_30 AC 1,043 ms
45,952 KB
testcase_31 AC 1,247 ms
57,848 KB
testcase_32 AC 28 ms
11,008 KB
testcase_33 AC 29 ms
11,136 KB
testcase_34 AC 66 ms
12,416 KB
testcase_35 AC 180 ms
14,336 KB
testcase_36 AC 51 ms
12,928 KB
testcase_37 AC 355 ms
18,944 KB
testcase_38 AC 280 ms
40,320 KB
testcase_39 AC 911 ms
49,640 KB
testcase_40 AC 55 ms
12,928 KB
testcase_41 AC 68 ms
11,776 KB
testcase_42 AC 184 ms
14,464 KB
testcase_43 AC 106 ms
14,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""
Strongly Connected Component
source ; https://github.com/ryusuke920/kyopro_educational_90_python/blob/main/solve_python/021.py
"""

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)

	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

	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())

	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

	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 range(m):
    a,b = map(int,input().split())
    scc.add_edge(a-1,b-1)
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 set(DAG[i]):
		source[i] = sink[j] = False
ans = max(sum(source),sum(sink))

print(ans)
0