結果
| 問題 |
No.2523 Trick Flower
|
| コンテスト | |
| ユーザー |
kwm_t
|
| 提出日時 | 2023-10-29 19:37:52 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 139 ms / 4,000 ms |
| コード長 | 2,130 bytes |
| コンパイル時間 | 3,721 ms |
| コンパイル使用メモリ | 260,724 KB |
| 最終ジャッジ日時 | 2025-02-17 16:52:36 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 31 |
ソースコード
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
//using mint = modint998244353;
//const int mod = 998244353;
//using mint = modint1000000007;
//const int mod = 1000000007;
//const int INF = 1e9;
//const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
vector<vector<int>> f(vector<vector<int>>&scc, vector<vector<int>>&to) {
int n = to.size();
int m = scc.size();
vector<int>v(n);
rep(i, m)rep(j, scc[i].size()) v[scc[i][j]] = i;
vector<vector<int>>res(m);
rep(i, m) {
for (int j : scc[i]) {
for (int k : to[j]) {
int l = v[k];
if (i == l)continue;
res[i].push_back(l);
}
}
std::sort(res[i].begin(), res[i].end());
res[i].erase(unique(res[i].begin(), res[i].end()), res[i].end());
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
vector<long long>a(n), b(n), c(n);
rep(i, n)cin >> a[i];
rep(i, n)cin >> b[i];
scc_graph g(n);
vector<vector<int>>to(n);
rep(i, n) {
int c; cin >> c; c--;
to[c].push_back(i);
g.add_edge(c, i);
}
auto scc = g.scc();
int sz = scc.size();
to = f(scc, to);
long long ok = 0;
long long ng = accumulate(all(a), 0LL) / accumulate(all(b), 0LL) + 1;
while (1 < abs(ok - ng)) {
long long mid = (ng + ok) / 2;
auto check = [&]() {
vector<long long>dp(sz);
rrep(i, sz) {
for (int j : to[i]) if (dp[j] < 0) dp[i] += dp[j], dp[j] = 0;
for (int j : scc[i]) dp[i] += a[j] - mid * b[j];
}
return all_of(all(dp), [](long long x) { return x >= 0; });
};
if (check()) ok = mid;
else ng = mid;
}
cout << ok << endl;
return 0;
}
kwm_t