結果

問題 No.2523 Trick Flower
ユーザー MasKoaTSMasKoaTS
提出日時 2023-03-30 18:49:57
言語 PyPy3
(7.3.15)
結果
MLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,405 bytes
コンパイル時間 337 ms
コンパイル使用メモリ 81,820 KB
実行使用メモリ 545,400 KB
最終ジャッジ日時 2023-10-25 21:08:15
合計ジャッジ時間 8,050 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
58,024 KB
testcase_01 AC 37 ms
53,556 KB
testcase_02 AC 38 ms
53,556 KB
testcase_03 AC 37 ms
53,556 KB
testcase_04 AC 37 ms
53,556 KB
testcase_05 AC 37 ms
53,556 KB
testcase_06 AC 38 ms
53,556 KB
testcase_07 AC 39 ms
53,556 KB
testcase_08 AC 81 ms
76,264 KB
testcase_09 AC 82 ms
76,316 KB
testcase_10 AC 44 ms
61,000 KB
testcase_11 AC 59 ms
68,332 KB
testcase_12 AC 53 ms
66,000 KB
testcase_13 AC 74 ms
76,012 KB
testcase_14 AC 67 ms
72,608 KB
testcase_15 MLE -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
import pypyjit
pypyjit.set_param('max_unroll_recursion=-1')

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(self.used[v]):
				continue
			self._dfs(v)
		for v in reversed(self.post_order):
			if(self.labels[v] != -1):
				continue
			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 = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
c = list(map(int, input().split()))

scc = SCC(N)
for i, k in enumerate(c):
	scc.add_edge(k - 1, i)
scc.build()
dag, groups = scc.construct()

n = len(groups)
a = [0] * n
b = [0] * n
for i, g in enumerate(groups):
	a[i] = sum(A[k] for k in g)
	b[i] = sum(B[k] for k in g)

def check(mid):
	visited = set([])
	def get_dp(x):
		nonlocal visited;	visited.add(x)
		ret = a[x] - mid * b[x]
		for y in dag[x]:
			ret += get_dp(y)
		return min(ret, 0)
	for i in range(n):
		if(i in visited or get_dp(i) == 0):
			continue
		return False
	return True

ok = 0
ng = sum(a) // sum(b) + 1
while(ng - ok > 1):
	mid = (ok + ng) >> 1
	if(check(mid)):
		ok = mid
	else:
		ng = mid
print(ok)
0