結果

問題 No.2523 Trick Flower
ユーザー MasKoaTSMasKoaTS
提出日時 2023-10-26 16:56:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 707 ms / 4,000 ms
コード長 2,405 bytes
コンパイル時間 446 ms
コンパイル使用メモリ 81,824 KB
実行使用メモリ 260,892 KB
最終ジャッジ日時 2023-10-26 16:56:38
合計ジャッジ時間 13,529 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,584 KB
testcase_01 AC 36 ms
53,584 KB
testcase_02 AC 37 ms
53,584 KB
testcase_03 AC 37 ms
53,584 KB
testcase_04 AC 36 ms
53,584 KB
testcase_05 AC 36 ms
53,584 KB
testcase_06 AC 35 ms
53,584 KB
testcase_07 AC 38 ms
53,584 KB
testcase_08 AC 79 ms
76,644 KB
testcase_09 AC 69 ms
72,836 KB
testcase_10 AC 50 ms
63,936 KB
testcase_11 AC 54 ms
66,080 KB
testcase_12 AC 53 ms
66,080 KB
testcase_13 AC 67 ms
72,692 KB
testcase_14 AC 65 ms
72,692 KB
testcase_15 AC 707 ms
260,892 KB
testcase_16 AC 660 ms
260,148 KB
testcase_17 AC 285 ms
135,412 KB
testcase_18 AC 248 ms
123,232 KB
testcase_19 AC 631 ms
165,472 KB
testcase_20 AC 637 ms
189,728 KB
testcase_21 AC 580 ms
185,216 KB
testcase_22 AC 630 ms
212,104 KB
testcase_23 AC 610 ms
168,024 KB
testcase_24 AC 355 ms
144,032 KB
testcase_25 AC 565 ms
211,296 KB
testcase_26 AC 499 ms
176,124 KB
testcase_27 AC 590 ms
211,468 KB
testcase_28 AC 470 ms
162,008 KB
testcase_29 AC 171 ms
89,684 KB
testcase_30 AC 424 ms
162,624 KB
testcase_31 AC 127 ms
78,816 KB
testcase_32 AC 278 ms
120,476 KB
testcase_33 AC 571 ms
177,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class SCC:
	def __init__(self, n):
		self.n = n
		self.route = [[] for _ in [0] * n]
		self.rev_route = [[] for _ in [0] * n]
		self.label = [-1] * n
		self.count = 0
	
	def add_edge(self, v_first, v_second):
		self.route[v_first].append(v_second)
		self.rev_route[v_second].append(v_first)

	def _build(self):
		self.v_list = []
		self.visited = set([])
		for v in range(self.n):
			if(v in self.visited):
				continue
			self._dfs(v)
		for v in reversed(self.v_list):
			if(self.label[v] != -1):
				continue
			self._rev_dfs(v)
			self.count += 1

	def _dfs(self, v_start):
		stack = [v_start, 0]
		while(stack):
			v, id = stack[-2:]
			if(id == 0 and v in self.visited):
				stack.pop()
				stack.pop()
				continue
			self.visited.add(v)
			if(id < len(self.route[v])):
				stack[-1] += 1
				stack.extend([self.route[v][id], 0])
				continue
			stack.pop()
			self.v_list.append(stack.pop())

	def _rev_dfs(self, v_start):
		stack = [v_start]
		self.label[v_start] = self.count
		while(stack):
			v_now = stack.pop()
			for v_next in self.rev_route[v_now]:
				if(self.label[v_next] != -1):
					continue
				self.label[v_next] = self.count
				stack.append(v_next)

	def construct(self):
		self._build()
		dag = [[] for _ in [0] * self.count]
		groups = [[] for _ in [0] * self.count]
		for v, lb in enumerate(self.label):
			for v_next in self.route[v]:
				lb_next = self.label[v_next]
				if(lb == lb_next):
					continue
				dag[lb].append(lb_next)
			groups[lb].append(v)
		return dag, groups



"""
Main Code
"""

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = [x - 1 for x in map(int, input().split())]

scc = SCC(N)
for i, x in enumerate(C):
	if(i == x):
		continue
	scc.add_edge(x, i)
dag, groups = scc.construct()

n = len(groups)
a = [0] * n
b = [0] * n
for i, group in enumerate(groups):
	a[i] = sum(A[x] for x in group)
	b[i] = sum(B[x] for x in group)
rdag = [[] for _ in [0] * n]
for x, ys in enumerate(dag):
	for y in ys:
		rdag[y].append(x)

def check(mid):
	dp = [x - mid * y for x, y in zip(a, b)]
	for x in range(n - 1, -1, -1):
		if(not(rdag[x]) and dp[x] < 0):
			return False
		if(dp[x] > 0):
			dp[x] = 0
		for y in rdag[x]:
			dp[y] += dp[x]
	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