QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#883803#6298. ColoringAMATSUKAZEWA 11ms17536kbC++204.7kb2025-02-05 19:02:242025-02-05 19:02:26

Judging History

This is the latest submission verdict.

  • [2025-02-05 19:02:26]
  • Judged
  • Verdict: WA
  • Time: 11ms
  • Memory: 17536kb
  • [2025-02-05 19:02:24]
  • Submitted

answer

#include<bits/stdc++.h>
#define rep(i,s,n) for (int i = (int)(s); i < (int)(n); i++)
#define all(v) begin(v),end(v)
using namespace std;
using ll = long long;
bool chmin(auto &a, auto b){ return a > b ? a = b, 1 : 0; }
bool chmax(auto &a, auto b){ return a < b ? a = b, 1 : 0; }

int main(){
    cin.tie(0)->sync_with_stdio(0);

    int n, st; cin >> n >> st;
    st--;
    vector<ll> w(n);
    rep(i,0,n) cin >> w[i];
    
    vector ikeru(n, vector<int>(0));
    vector<ll> p(n);
    rep(i,0,n) cin >> p[i];

    vector<int> deg(n);
    vector<int> a(n);
    rep(i,0,n) {
        cin >> a[i];
        a[i]--;
        ikeru[a[i]].push_back(i);
        deg[a[i]]++;
    }

    vector<int> mada;
    rep(i,0,n) {
        if (deg[i] == 0) {
            mada.push_back(i);
        }
    }

    vector<bool> is_cycle(n, true);
    while(!mada.empty()){
        int i = mada.back();
        mada.pop_back();
        is_cycle[i] = false;
        deg[a[i]]--;
        if(deg[a[i]] == 0) {
            mada.push_back(a[i]);
        }
    }

    vector child_tree(n, vector<int>(0));    
    vector dp(n, vector<ll>(1)); // total profit of each subtree (i), alternating white and black (j) times.
    auto dfs = [&](auto self, int i) -> void {
        vector<ll> tmp(2);
        tmp[0] = 0;
        tmp[1] = w[i];
        for (int j: ikeru[i]) {
            self(self, j);
            int sx = (int)tmp.size();
            int sy = (int)dp[j].size();
            vector<ll> ntmp(max(sx, sy + 1), -2e18);
            rep(x,0,sx) {
                rep(y,0,sy) {
                    if ((i+j)%2 == 0) {
                        chmax(ntmp[max(x, y)], tmp[x] + dp[j][y]);
                    }else{
                        chmax(ntmp[max(x, y + 1)], tmp[x] + dp[j][y]);
                    }
                }
            }
            swap(ntmp, tmp);
        }
        rep(x,0,(int)tmp.size()) {
            tmp[x] -= p[i] * x;
        }
        swap(dp[i], tmp);
    };

    rep(i,0,n) {
        if (is_cycle[i]) {
            for (int j: ikeru[i]) {
                if (is_cycle[j]) continue;
                dfs(dfs, j);
                child_tree[i].push_back(j);
            }
        }
    }

    ll ans = -4e18;
    if (!is_cycle[st]) {
        chmax(ans, dp[st][1] + p[st]);
        if ((int)dp[st].size() >= 3) {
            chmax(ans, dp[st][2] + p[st]);
        }
        cout << ans << '\n';
        return 0;
    }

    rep(i,0,n) {
        if (is_cycle[i]) {
            for (int j: child_tree[i]) {
                rep(k,0,(int)dp[j].size()-1) {
                    chmax(dp[j][k+1], dp[j][k]);
                }
            }
        }
    }

    vector<int> cycle_list;
    int now = st;
    do {
        cycle_list.push_back(now);
        for (int j: ikeru[now]) {
            if (is_cycle[j]) {
                now = j;
                break;
            }
        }
    } while(now != st);

    // cycle junkai
    int m = (int)cycle_list.size();

    auto get = [&](int i, int j) -> ll {
        ll ret = 0;
        for (int x: child_tree[i]) {
            ret += dp[x][min(j,(int)dp[x].size()-1)];
        }
        return ret;
    };

    vector<ll> tmp(m+1);
    int sz = 1<<13;
    vector<ll> seg(2*sz, -3e18);
    auto seg_set = [&](int i) -> void {
        seg[i+sz] = tmp[i];
        i += sz;
        i >>= 1;
        while(i > 0) {
            seg[i] = max(seg[i*2], seg[i*2+1]);
            i >>= 1;
        }
    };

    ll geta = 0;
    seg_set(0);
    rep(i,0,m) {
        if (i > 0) geta -= p[cycle_list[i]];
        ll tar = get(cycle_list[i], 1) + w[cycle_list[i]];
        geta += tar;
        tmp[i+1] = tmp[i] + get(cycle_list[i], 2) - p[cycle_list[i]] - tar; 
        seg_set(i+1);
        /*
        rep(j,0,m+1) {
            cout << tmp[j] + geta << ' ';
        }
        cout << endl;
        */
        chmax(ans, seg[1] + geta);
    }

    vector alls(n+4, vector<ll>(m+1));
    rep(i,0,n+4){
        rep(j,0,m) {
            alls[i][j+1] += alls[i][j];
            alls[i][j+1] += get(cycle_list[j], 2*i) - p[cycle_list[j]] * (2*i) + (j==0?p[cycle_list[j]]:0);
        }
    }

    rep(i,0,(n+1)*m) {
        ll tar1 = get(cycle_list[i%m], 2*(i/m)+3) - get(cycle_list[i%m], 2*(i/m)+2) + w[cycle_list[i%m]];
        geta += tar1;
        geta -= p[cycle_list[i%m]];
        tmp[i%(m+1)] = alls[(i/m)+2][i%m+1] - alls[(i/m)+2][0] + alls[(i/m)+1][m] - alls[(i/m)+1][i%m+1] - geta;
        seg_set(i%(m+1));
        /*
        rep(j,0,m+1) {
            cout << tmp[j]+geta << ' ';
        }
        cout << endl;
        */
        chmax(ans, seg[1] + geta);
    }
    
    cout << ans << endl;



}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3712kb

input:

3 1
-1 -1 2
1 0 0
3 1 2

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3840kb

input:

10 8
36175808 53666444 14885614 -14507677 -92588511 52375931 -87106420 -7180697 -158326918 98234152
17550389 45695943 55459378 18577244 93218347 64719200 84319188 34410268 20911746 49221094
8 1 2 2 8 8 4 7 8 4

output:

35343360

result:

ok 1 number(s): "35343360"

Test #3:

score: -100
Wrong Answer
time: 11ms
memory: 17536kb

input:

5000 1451
531302480 400140870 -664321146 -376787089 -440627168 -672055995 924309614 2764785 -225700856 880835131 -435550509 162278080 -635253658 251803267 -499868931 213283508 603121701 -603347266 541062018 -502078443 -585620031 486788884 864390909 -670529282 -63580194 512939729 691685896 481123612 ...

output:

-1999999998147206091

result:

wrong answer 1st numbers differ - expected: '83045140866', found: '-1999999998147206091'