結果

問題 No.1813 Magical Stones
ユーザー MasKoaTSMasKoaTS
提出日時 2021-09-19 13:02:22
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,537 ms / 2,000 ms
コード長 1,906 bytes
コンパイル時間 177 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 59,136 KB
最終ジャッジ日時 2024-07-16 06:55:31
合計ジャッジ時間 26,433 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
11,008 KB
testcase_01 AC 33 ms
11,008 KB
testcase_02 AC 28 ms
11,008 KB
testcase_03 AC 353 ms
55,680 KB
testcase_04 AC 29 ms
11,136 KB
testcase_05 AC 29 ms
11,008 KB
testcase_06 AC 54 ms
13,184 KB
testcase_07 AC 30 ms
11,136 KB
testcase_08 AC 32 ms
11,392 KB
testcase_09 AC 36 ms
11,776 KB
testcase_10 AC 37 ms
11,264 KB
testcase_11 AC 266 ms
26,112 KB
testcase_12 AC 31 ms
11,008 KB
testcase_13 AC 30 ms
11,136 KB
testcase_14 AC 366 ms
55,808 KB
testcase_15 AC 1,495 ms
59,136 KB
testcase_16 AC 1,472 ms
59,136 KB
testcase_17 AC 1,319 ms
50,336 KB
testcase_18 AC 1,471 ms
59,008 KB
testcase_19 AC 1,537 ms
59,136 KB
testcase_20 AC 1,452 ms
59,008 KB
testcase_21 AC 1,475 ms
59,008 KB
testcase_22 AC 1,428 ms
58,836 KB
testcase_23 AC 1,480 ms
59,136 KB
testcase_24 AC 39 ms
11,392 KB
testcase_25 AC 163 ms
20,480 KB
testcase_26 AC 70 ms
14,080 KB
testcase_27 AC 972 ms
47,104 KB
testcase_28 AC 1,179 ms
41,328 KB
testcase_29 AC 1,160 ms
48,496 KB
testcase_30 AC 1,108 ms
45,312 KB
testcase_31 AC 1,343 ms
57,344 KB
testcase_32 AC 31 ms
11,008 KB
testcase_33 AC 30 ms
11,008 KB
testcase_34 AC 66 ms
12,416 KB
testcase_35 AC 181 ms
14,336 KB
testcase_36 AC 52 ms
13,056 KB
testcase_37 AC 373 ms
19,072 KB
testcase_38 AC 302 ms
40,320 KB
testcase_39 AC 976 ms
48,888 KB
testcase_40 AC 55 ms
12,800 KB
testcase_41 AC 69 ms
11,776 KB
testcase_42 AC 195 ms
14,464 KB
testcase_43 AC 112 ms
14,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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 DAG[i]:
		sink[i] = source[j] = False
source_cnt = sum(i for i in source)
sink_cnt = sum(i for i in sink)
ans = max(source_cnt,sink_cnt)

print(ans)
0