Submission #791780

# Submission time Handle Problem Language Result Execution time Memory
791780 2023-07-24T09:53:49 Z phoebe Islands (IOI08_islands) C++17
90 / 100
596 ms 131072 KB
#include <bits/stdc++.h>
using namespace std;

// #define int long long
#define ll long long
#define pii pair<ll, int>
#define F first
#define S second
#define PB push_back
#define ALL(x) x.begin(), x.end()
#define FOR(i, n) for (int i = 0; i < n; i++)
#define NYOOM ios::sync_with_stdio(0); cin.tie(0);
#define endl '\n'
const int INF = 1e9 + 7;
const ll LLINF = 1ll<<60;

const int maxn = 1e6 + 10;
int n, p[maxn], l[maxn], seen[maxn] = {0}; // 0 = unseen, 1 = seeing, 2 = seen
ll val[maxn], cur = 0, re = 0; // val[v][i] = node v connected to i nodes already
// val[v][2] >= val[v][1] >= val[v][0]
bool on_cycle[maxn] = {0};
vector<int> adj[maxn];

void dfs(int v, int p){
    val[v] = 0;
    ll best = -1, second_best = -1;
    for (auto u : adj[v]){
        if (on_cycle[u] || u == p) continue;
        dfs(u, v);
        val[v] = max(val[v], val[u] + l[u]);
        if (best == -1) best = val[u] + l[u];
        else second_best = max(second_best, val[u] + l[u]);
        if (second_best > best) swap(best, second_best);
    }
    cur = max(cur, best + second_best);
}

void solve(int s, int sz, ll total_sum){
    int v = s;
    // forward
    ll cw = -LLINF, ccw = -LLINF, cur_sum = 0;
    FOR(i, sz){
        cur = max(cur, cw + val[v] + cur_sum);
        cur = max(cur, ccw + val[v] + (total_sum - cur_sum));
        cw = max(cw, val[v] - cur_sum);
        ccw = max(ccw, val[v] + cur_sum);
        cur_sum += l[v]; v = p[v];
    }
}

void find_cycle(int s){
    int v = s; 
    while (seen[v] == 0){ // while unseen
        seen[v] = 1; v = p[v];
    }
    if (seen[v] == 1){ // found a cycle
        cur = 0; int sz = 0; ll total_sum = 0;
        while (!on_cycle[v]){
            total_sum += l[v];
            sz++; on_cycle[v] = true; v = p[v];
        }
        FOR(i, sz){
            dfs(v, v); v = p[v];
        }
        solve(v, sz, total_sum);
        re += cur;
    }
    v = s; 
    while (seen[v] != 2){
        seen[v] = 2; v = p[v];
    }
}

signed main(){
    NYOOM;
    // freopen("isl.in", "r", stdin);
    // freopen("isl.out", "w", stdout);
    cin >> n;
    for (int i = 1; i <= n; i++){
        cin >> p[i] >> l[i];
        adj[p[i]].PB(i);
    }
    for (int i = 1; i <= n; i++){
        if (seen[i] != 2) find_cycle(i);
    }
    cout << re;
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23788 KB Output is correct
2 Correct 11 ms 23764 KB Output is correct
3 Correct 11 ms 23824 KB Output is correct
4 Correct 11 ms 23812 KB Output is correct
5 Correct 13 ms 23816 KB Output is correct
6 Correct 11 ms 23736 KB Output is correct
7 Correct 11 ms 23764 KB Output is correct
8 Correct 11 ms 23800 KB Output is correct
9 Correct 11 ms 23764 KB Output is correct
10 Correct 13 ms 23816 KB Output is correct
11 Correct 11 ms 23812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23824 KB Output is correct
2 Correct 12 ms 23832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23880 KB Output is correct
2 Correct 13 ms 24012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 24596 KB Output is correct
2 Correct 19 ms 25432 KB Output is correct
3 Correct 17 ms 25004 KB Output is correct
4 Correct 16 ms 24412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 26256 KB Output is correct
2 Correct 31 ms 29208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 33740 KB Output is correct
2 Correct 51 ms 37512 KB Output is correct
3 Correct 63 ms 37280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 88 ms 45868 KB Output is correct
2 Correct 107 ms 47528 KB Output is correct
3 Correct 112 ms 59888 KB Output is correct
4 Correct 138 ms 58464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 171 ms 62820 KB Output is correct
2 Correct 360 ms 75252 KB Output is correct
3 Correct 164 ms 65708 KB Output is correct
4 Correct 221 ms 74900 KB Output is correct
5 Correct 203 ms 72084 KB Output is correct
6 Correct 596 ms 77668 KB Output is correct
7 Correct 225 ms 77768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 212 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -