結果

問題 No.2523 Trick Flower
ユーザー MasKoaTSMasKoaTS
提出日時 2023-03-29 18:23:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 193 ms / 4,000 ms
コード長 2,174 bytes
コンパイル時間 5,217 ms
コンパイル使用メモリ 225,044 KB
実行使用メモリ 41,756 KB
最終ジャッジ日時 2023-10-25 21:08:29
合計ジャッジ時間 6,948 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 3 ms
4,348 KB
testcase_09 AC 3 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 193 ms
41,756 KB
testcase_16 AC 173 ms
36,028 KB
testcase_17 AC 148 ms
23,208 KB
testcase_18 AC 134 ms
23,176 KB
testcase_19 AC 170 ms
26,600 KB
testcase_20 AC 175 ms
26,548 KB
testcase_21 AC 172 ms
26,612 KB
testcase_22 AC 170 ms
26,516 KB
testcase_23 AC 174 ms
26,580 KB
testcase_24 AC 88 ms
16,668 KB
testcase_25 AC 138 ms
23,932 KB
testcase_26 AC 131 ms
22,972 KB
testcase_27 AC 151 ms
25,436 KB
testcase_28 AC 124 ms
21,584 KB
testcase_29 AC 30 ms
7,844 KB
testcase_30 AC 107 ms
19,340 KB
testcase_31 AC 9 ms
4,800 KB
testcase_32 AC 61 ms
12,924 KB
testcase_33 AC 154 ms
26,264 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;
}


vector<vector<int> > get_rev_graph(int n, vector<vector<int> >& route) {
	vector<vector<int> > ret(n);
	for (int i = 0; i < n; i++) {
		for (int x : route[i]) {
			ret[x].push_back(i);
		}
	}

	return ret;
}


bool check(ll mid, int n, vector<ll>& a, vector<ll>& b, vector<vector<int> >& rdag) {
	vector<ll> dp(n);
	for (int i = 0; i < n; i++) {
		dp[i] = a[i] - b[i] * mid;
	}
	for (int i = n - 1; i >= 0; i--) {
		if (dp[i] >= 0) {
			dp[i] = 0;
			continue;
		}
		if (rdag[i].empty()) {
			return false;
		}
		for (int x : rdag[i]) {
			dp[x] += dp[i];
		}
	}
	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++) {
		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();
	vector<vector<int> > rdag = get_rev_graph(n, dag);

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

	return 0;
}
0