結果

問題 No.2523 Trick Flower
ユーザー MasKoaTSMasKoaTS
提出日時 2023-10-26 20:36:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 589 ms / 4,000 ms
コード長 2,466 bytes
コンパイル時間 3,002 ms
コンパイル使用メモリ 226,392 KB
実行使用メモリ 37,476 KB
最終ジャッジ日時 2023-10-26 20:36:13
合計ジャッジ時間 8,105 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 2 ms
4,372 KB
testcase_03 AC 2 ms
4,372 KB
testcase_04 AC 2 ms
4,372 KB
testcase_05 AC 2 ms
4,372 KB
testcase_06 AC 2 ms
4,372 KB
testcase_07 AC 2 ms
4,372 KB
testcase_08 AC 3 ms
4,372 KB
testcase_09 AC 3 ms
4,372 KB
testcase_10 AC 2 ms
4,372 KB
testcase_11 AC 2 ms
4,372 KB
testcase_12 AC 2 ms
4,372 KB
testcase_13 AC 3 ms
4,372 KB
testcase_14 AC 3 ms
4,372 KB
testcase_15 AC 568 ms
37,476 KB
testcase_16 AC 589 ms
33,936 KB
testcase_17 AC 140 ms
19,780 KB
testcase_18 AC 125 ms
17,800 KB
testcase_19 AC 168 ms
22,148 KB
testcase_20 AC 196 ms
22,144 KB
testcase_21 AC 216 ms
22,152 KB
testcase_22 AC 175 ms
22,148 KB
testcase_23 AC 166 ms
22,144 KB
testcase_24 AC 85 ms
14,416 KB
testcase_25 AC 113 ms
20,128 KB
testcase_26 AC 146 ms
19,400 KB
testcase_27 AC 127 ms
21,184 KB
testcase_28 AC 132 ms
18,280 KB
testcase_29 AC 24 ms
6,964 KB
testcase_30 AC 97 ms
16,468 KB
testcase_31 AC 8 ms
4,612 KB
testcase_32 AC 51 ms
10,960 KB
testcase_33 AC 143 ms
21,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/scc>
using namespace std;
using namespace atcoder;
using ll = long long;


vector<vector<int> > get_DAG_from_SCC(int n, vector<ll>& a, vector<ll>& b, vector<vector<int> >& graph, vector<vector<int> >& scc) {
    int sz = (int)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 k : graph[i]) {
            int x = group[i], y = group[k];
            if (x != y) {
                dag[x].push_back(y);
            }
        }
    }
    for (vector<int>& vec : dag) {
        sort(vec.begin(), vec.end());
        vec.erase(unique(vec.begin(), vec.end()), vec.end());
    }

    return dag;
}


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


bool check(ll mid, int n, vector<ll>& a, vector<ll>& b, vector<vector<int> >& dag) {
    vector<bool> visited(n);
    for (int v_start = 0; v_start < n; ++v_start) {
        if (visited[v_start] || get_dp(mid, n, a, b, dag, visited, v_start) == 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(auto& x : a){
        cin >> x;    sa += x;
    }
    for(auto& x : b){
        cin >> x;    sb += x;
    }
    for(auto& x : c){
        cin >> x;    --x;
    }

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

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

    return 0;
}
0