結果

問題 No.2523 Trick Flower
ユーザー MasKoaTSMasKoaTS
提出日時 2023-03-31 19:52:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 849 ms / 4,000 ms
コード長 2,140 bytes
コンパイル時間 3,523 ms
コンパイル使用メモリ 228,868 KB
実行使用メモリ 40,208 KB
最終ジャッジ日時 2023-10-25 21:07:27
合計ジャッジ時間 10,115 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 3 ms
4,348 KB
testcase_09 AC 4 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 3 ms
4,348 KB
testcase_14 AC 3 ms
4,348 KB
testcase_15 AC 849 ms
40,208 KB
testcase_16 AC 843 ms
39,760 KB
testcase_17 AC 151 ms
21,620 KB
testcase_18 AC 124 ms
17,776 KB
testcase_19 AC 240 ms
25,012 KB
testcase_20 AC 346 ms
24,996 KB
testcase_21 AC 339 ms
25,092 KB
testcase_22 AC 243 ms
24,976 KB
testcase_23 AC 244 ms
25,032 KB
testcase_24 AC 120 ms
15,608 KB
testcase_25 AC 111 ms
20,104 KB
testcase_26 AC 240 ms
21,120 KB
testcase_27 AC 129 ms
24,112 KB
testcase_28 AC 198 ms
19,924 KB
testcase_29 AC 24 ms
6,940 KB
testcase_30 AC 122 ms
17,780 KB
testcase_31 AC 10 ms
4,628 KB
testcase_32 AC 53 ms
11,864 KB
testcase_33 AC 169 ms
24,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/scc>
#define all(x) x.begin(), x.end()
using namespace std;
using namespace atcoder;
using ll = long long;


vector<vector<int> > convert_SCC_to_DAG(int n, vector<ll>& a, vector<ll>& b, vector<vector<int> >& route, vector<vector<int> >& scc) {
	int sz = scc.size();
	vector<ll> p(sz), q(sz);

	vector<vector<int> > dag(sz, vector<int>({}));
	vector<int> group(n);
	for (int i = 0; i < sz; i++) {
		for (int x : scc[i]) {
			group[x] = i;
			p[i] += a[x];	q[i] += b[x];
		}
	}
	a = p;	b = q;

	for (int i = 0; i < n; i++) {
		for (int x : route[i]) {
			int a = group[i];	int b = group[x];
			if (a != b) {
				dag[a].push_back(b);
			}
		}
	}
	for (vector<int>& v : dag) {
		sort(all(v));	v.erase(unique(all(v)), v.end());
	}

	return dag;
}


ll get_dp(ll mid, int n, vector<ll>& a, vector<ll>& b, vector<vector<int> >& dag, unordered_set<int>& visited, int x) {
	visited.insert(x);
	ll ret = a[x] - b[x] * mid;
	for (int k : dag[x]) {
		ret += get_dp(mid, n, a, b, dag, visited, k);
	}
	return min(ret, 0ll);
}


bool check(ll mid, int n, vector<ll>& a, vector<ll>& b, vector<vector<int> >& dag) {
	unordered_set<int> visited = {};
	for (int i = 0; i < n; i++) {
		if (visited.find(i) != visited.end() or get_dp(mid, n, a, b, dag, visited, i) >= 0) {
			continue;
		}
		return false;
	}
	return true;
}


int main(void) {
	int n;	cin >> n;
	vector<ll> a(n), b(n);
	vector<int> c(n);
	ll sa = 0, sb = 0;
	for (int i = 0; i < n; i++) {
		cin >> a[i];	sa += a[i];
	}
	for (int i = 0; i < n; i++) {
		cin >> b[i];	sb += b[i];
	}
	for (int i = 0; i < n; i++) {
		cin >> c[i];	c[i]--;
	}

	vector<vector<int> > route(n, vector<int>({}));
	scc_graph g(n);
	for (int i = 0; i < n; i++) {
		if (i == c[i]) {
			continue;
		}
		route[c[i]].push_back(i);
		g.add_edge(c[i], i);
	}
	vector<vector<int> > scc = g.scc();
	vector<vector<int> > dag = convert_SCC_to_DAG(n, a, b, route, scc);
	n = scc.size();

	ll ok = 0;
	ll ng = sa / sb + 1;
	while (ng - ok > 1) {
		ll mid = (ok + ng) >> 1ll;
		if (check(mid, n, a, b, dag)) {
			ok = mid;
		}
		else {
			ng = mid;
		}
	}
	cout << ok << endl;

	return 0;
}
0