結果

問題 No.2523 Trick Flower
ユーザー MasKoaTSMasKoaTS
提出日時 2023-04-01 17:32:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 955 ms / 4,000 ms
コード長 2,468 bytes
コンパイル時間 433 ms
コンパイル使用メモリ 82,128 KB
実行使用メモリ 261,912 KB
最終ジャッジ日時 2024-09-25 05:56:15
合計ジャッジ時間 14,090 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,492 KB
testcase_01 AC 38 ms
53,224 KB
testcase_02 AC 43 ms
53,084 KB
testcase_03 AC 40 ms
52,900 KB
testcase_04 AC 41 ms
53,296 KB
testcase_05 AC 40 ms
53,820 KB
testcase_06 AC 40 ms
53,076 KB
testcase_07 AC 41 ms
53,852 KB
testcase_08 AC 84 ms
76,944 KB
testcase_09 AC 79 ms
76,888 KB
testcase_10 AC 55 ms
65,632 KB
testcase_11 AC 58 ms
67,284 KB
testcase_12 AC 58 ms
66,316 KB
testcase_13 AC 78 ms
76,684 KB
testcase_14 AC 73 ms
73,976 KB
testcase_15 AC 955 ms
261,912 KB
testcase_16 AC 891 ms
261,148 KB
testcase_17 AC 311 ms
149,188 KB
testcase_18 AC 268 ms
137,796 KB
testcase_19 AC 787 ms
221,860 KB
testcase_20 AC 817 ms
260,720 KB
testcase_21 AC 756 ms
257,368 KB
testcase_22 AC 836 ms
261,056 KB
testcase_23 AC 763 ms
226,020 KB
testcase_24 AC 515 ms
187,076 KB
testcase_25 AC 668 ms
260,744 KB
testcase_26 AC 613 ms
233,632 KB
testcase_27 AC 680 ms
259,316 KB
testcase_28 AC 589 ms
221,152 KB
testcase_29 AC 203 ms
107,532 KB
testcase_30 AC 543 ms
228,904 KB
testcase_31 AC 131 ms
78,944 KB
testcase_32 AC 364 ms
172,420 KB
testcase_33 AC 743 ms
260,608 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 = [k - 1 for k in map(int, input().split())]

scc = SCC(N)
for i, k in enumerate(C):
	if(i == k):
		continue
	scc.add_edge(k, i)
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)

rdag = [[] for _ in [0] * n]
for i, lis in enumerate(dag):
	for v in lis:
		rdag[v].append(i)

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