Submission #890159

# Submission time Handle Problem Language Result Execution time Memory
890159 2023-12-20T16:03:54 Z Macker Islands (IOI08_islands) C++17
9 / 100
285 ms 131072 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define all(v) v.begin(), v.end()

struct node{
    vector<pair<ll, ll>> adj = {};
    vector<ll> binJmp;
    vector<ll> binDp;
    vector<ll> binSum;
    bool isCyc = 0;
    ll mxd = 0;
    ll mxch = 0;
    ll treeDp = 0;
    ll vis = 0;
};

vector<node> v;
vector<vector<ll>> cycles;

ll precDfs(ll a, ll p){
    v[a].vis = 1;
    ll cyc = -1;
    for (auto &i : v[a].adj) {
        ll b = i.second;
        if(!v[b].vis){
            cyc = max(cyc, precDfs(b, a));
        }
        else if(v[b].vis && b != p && !v[b].isCyc){
            cyc = b;
            v[b].isCyc = 1;
            cycles.back().push_back(b);
        }
    }
    if(cyc == a) return -1;
    else if(cyc != -1) {
        v[a].isCyc = 1;
        cycles.back().push_back(a);
    }
    return cyc;
}

pair<ll, ll> ddfs(ll a){
    v[a].vis = 2;
    ll mxd = 0;
    ll mxch = a;
    for (auto &i : v[a].adj) {
        node b = v[i.second];
        if(b.vis < 2 && !b.isCyc){
            pair<ll, ll> nd = ddfs(i.second);
            if(mxd < nd.first + i.first){
               mxd = nd.first + i.first;
               mxch = nd.second;
            }
        }
    }
    v[a].mxd = mxd;
    v[a].mxch = mxch;
    return {mxd, mxch};
}

ll tdpDfs(ll a){
    v[a].vis = 3;
    ll dp = 0;
    for (auto &i : v[a].adj) {
        node b = v[i.second];
        if(b.vis < 3 && !(b.isCyc && v[a].isCyc)){
            dp = max(tdpDfs(i.second) + i.first, dp);
        }
    }
    return dp;
}

int main()
{
    ll n; cin >> n;
    v.resize(n);
    for (ll i = 0; i < n; i++) {    
        ll a, l; cin >> a >> l; a--;
        v[i].binDp.push_back(l);
        v[i].binSum.push_back(l);
        v[i].binJmp.push_back(a);
        v[i].adj.push_back({l, a});
        v[a].adj.push_back({l, i});
    }

    for (ll i = 0; i < n; i++) {
        v[i].binJmp.resize(22);
        v[i].binSum.resize(22);
        v[i].binDp.resize(22);
        if(!v[i].vis){
            cycles.push_back({});
            precDfs(i, -1);
        }
    }

    for (ll i = 0; i < n; i++) {
        if(v[i].vis < 2 && v[i].isCyc){
            pair<ll, ll> ret = ddfs(i);
            v[i].treeDp = tdpDfs(ret.second);
        }
    }
    
    ll res = 0;
    for (ll i = 0; i < cycles.size(); i++) {
        for (ll j = 0; j < cycles[i].size(); j++) {
            ll a = cycles[i][j];
            v[a].binDp[0] += v[v[a].binJmp[0]].mxd;
        }
        for (ll k = 1; k < 22; k++) {
            for (ll j = 0; j < cycles[i].size(); j++) {
                ll a = cycles[i][j];
                ll b = v[a].binJmp[k - 1];
                v[a].binSum[k] = v[a].binSum[k - 1] + v[b].binSum[k - 1];
                v[a].binJmp[k] = v[b].binJmp[k - 1];
                ll x = v[a].binSum[k - 1] + v[b].mxd;
                ll y = v[a].binSum[k - 1] + v[b].binDp[k - 1];
                v[a].binDp[k] = max(v[a].binDp[k - 1], max(x, y));
            }
        }
        ll cmx = 0;
        ll mxJmp = cycles[i].size() - 1;
        for (ll k = 0; k < cycles[i].size(); k++) {
            ll dp = 0;
            ll cur = cycles[i][k];
            ll s = 0;
            for (ll j = 21; j >= 0; j--) {
                if(mxJmp & (1 << j)) {
                    dp = max(dp, max(s + v[cur].binDp[j], (s + v[cur].mxd) * (cur != cycles[i][k])));
                    cur = v[cur].binJmp[j];
                    s += v[cur].binSum[j];
                }
            }
            cmx = max(cmx, dp + v[cycles[i][k]].mxd);
            cmx = max(cmx, v[cycles[i][k]].treeDp);
        }
        res += cmx;
    }
    cout << res << endl;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:106:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 |     for (ll i = 0; i < cycles.size(); i++) {
      |                    ~~^~~~~~~~~~~~~~~
islands.cpp:107:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |         for (ll j = 0; j < cycles[i].size(); j++) {
      |                        ~~^~~~~~~~~~~~~~~~~~
islands.cpp:112:30: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |             for (ll j = 0; j < cycles[i].size(); j++) {
      |                            ~~^~~~~~~~~~~~~~~~~~
islands.cpp:124:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  124 |         for (ll k = 0; k < cycles[i].size(); k++) {
      |                        ~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Incorrect 1 ms 600 KB Output isn't correct
3 Incorrect 1 ms 604 KB Output isn't correct
4 Incorrect 1 ms 344 KB Output isn't correct
5 Correct 0 ms 348 KB Output is correct
6 Incorrect 0 ms 348 KB Output isn't correct
7 Incorrect 1 ms 348 KB Output isn't correct
8 Incorrect 0 ms 348 KB Output isn't correct
9 Incorrect 0 ms 344 KB Output isn't correct
10 Incorrect 1 ms 348 KB Output isn't correct
11 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 1116 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 1884 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 29 ms 13024 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 118 ms 44016 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 217 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 285 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 38 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 30 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -