結果
問題 | No.2523 Trick Flower |
ユーザー |
![]() |
提出日時 | 2023-10-27 23:13:17 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,791 bytes |
コンパイル時間 | 2,442 ms |
コンパイル使用メモリ | 205,412 KB |
最終ジャッジ日時 | 2025-02-17 15:54:37 |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 WA * 1 |
ソースコード
#include <bits/stdc++.h> #ifdef LOCAL #include <debug.hpp> #else #define debug(...) void(0) #endif struct FunctionalGraph { int n, L; std::vector<int> visit; std::vector<std::vector<int>> loops, G; FunctionalGraph(int n) : n(n), L(1), visit(n), G(n), nxt(n, -1) {} void add_edge(int u, int v) { assert(0 <= u and u < n); assert(0 <= v and v < n); nxt[u] = v; } void build() { for (int i = 0; i < n; i++) { if (visit[i] != 0) continue; std::vector<int> loop; dfs(i, loop); if (not loop.empty()) loops.emplace_back(loop); } for (int i = 0; i < n; i++) { if (visit[i] < 0) { G[nxt[i]].emplace_back(i); } } } private: std::vector<int> nxt; void make_loop(int s, std::vector<int>& loop) { loop.emplace_back(s); visit[s] = L; for (int cur = nxt[s]; cur != s; cur = nxt[cur]) { loop.emplace_back(cur); visit[cur] = L; } } int dfs(int v, std::vector<int>& loop) { visit[v] = -L; int u = nxt[v]; if (visit[u] == -L) { make_loop(v, loop); L++; return 0; } else if (visit[u] == 0) { int res = dfs(u, loop); if (res == 0) return 0; return visit[v] = res; } return visit[v] = (visit[u] > 0 ? -visit[u] : visit[u]); } }; using namespace std; typedef long long ll; #define all(x) begin(x), end(x) constexpr int INF = (1 << 30) - 1; constexpr long long IINF = (1LL << 60) - 1; constexpr int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; template <class T> istream& operator>>(istream& is, vector<T>& v) { for (auto& x : v) is >> x; return is; } template <class T> ostream& operator<<(ostream& os, const vector<T>& v) { auto sep = ""; for (const auto& x : v) os << exchange(sep, " ") << x; return os; } template <class T, class U = T> bool chmin(T& x, U&& y) { return y < x and (x = forward<U>(y), true); } template <class T, class U = T> bool chmax(T& x, U&& y) { return x < y and (x = forward<U>(y), true); } template <class T> void mkuni(vector<T>& v) { sort(begin(v), end(v)); v.erase(unique(begin(v), end(v)), end(v)); } template <class T> int lwb(const vector<T>& v, const T& x) { return lower_bound(begin(v), end(v), x) - begin(v); } template <class T> T ceil(T x, T y) { assert(y >= 1); return (x > 0 ? (x + y - 1) / y : x / y); } template <class T> T floor(T x, T y) { assert(y >= 1); return (x > 0 ? x / y : (x - y + 1) / y); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<int> A(N), B(N), C(N); cin >> A >> B >> C; FunctionalGraph G(N); for (int i = 0; i < N; i++) G.add_edge(i, --C[i]); G.build(); auto g = G.G; auto loops = G.loops; auto dfs = [&](auto self, int v, ll x) -> ll { ll sum = A[v] - 1LL * B[v] * x; for (int& u : g[v]) { ll ch = self(self, u, x); if (ch < 0) sum += ch; } return sum; }; auto check = [&](ll x) -> bool { for (int i = 0; i < N; i++) { if (B[i] > 0 and ceil(IINF, 1LL * B[i]) <= x) { return false; } } for (auto& loop : loops) { ll sum = 0; for (int& r : loop) { ll val = dfs(dfs, r, x); sum += val; } if (sum < 0) return false; } return true; }; ll lb = 0, ub = IINF; while (ub - lb > 1) { ll mid = (lb + ub) >> 1; (check(mid) ? lb : ub) = mid; } cout << lb << '\n'; return 0; }