Submission #233417

# Submission time Handle Problem Language Result Execution time Memory
233417 2020-05-20T12:55:39 Z dolphingarlic Islands (IOI08_islands) C++14
76 / 100
618 ms 131076 KB
#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;

vector<pair<int, ll>> graph[1010101], cycle;
ll discarded, ans = 0, cmp_ans, dp[1010101], p[5][1010101];
int cycle_start;
bool processed[1010101], visited[1010101], on_cycle[1010101], adding = false;

void find_cycle(int node, pair<int, ll> parent = {0, 0}) {
    if (visited[node]) {
        cycle_start = node;
        cycle.push_back({node, 0});
        cycle.push_back(parent);
        adding = true;
        return;
    }
    visited[node] = true;
    for (pair<int, ll> i : graph[node]) if (i != parent) {
        find_cycle(i.first, {node, i.second});
        if (cycle_start) {
            if (node == cycle_start) adding = false;
            if (adding) cycle.push_back(parent);
            return;
        }
    }
}

void calc_tree(int node, int parent = 0) {
    processed[node] = true;
    ll mx = 0, sc = 0;
    for (pair<int, ll> i : graph[node]) if (i.first != parent && !on_cycle[i.first]) {
        calc_tree(i.first, node);
        if (dp[i.first] + i.second > mx) sc = mx, mx = dp[i.first] + i.second;
        else if (dp[i.first] + i.second > sc) sc = dp[i.first] + i.second;
    }
    dp[node] = mx;
    cmp_ans = max(cmp_ans, mx + sc);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    FOR(i, 1, n + 1) {
        int v;
        ll l;
        cin >> v >> l;
        graph[i].push_back({v, l});
        graph[v].push_back({i, l});
    }

    FOR(i, 1, n + 1) if (!processed[i]) {
        cycle.clear();
        cycle_start = 0;
        find_cycle(i);
        discarded = cycle.back().second;
        cycle.pop_back();

        for (pair<int, ll> j : cycle) on_cycle[j.first] = true;

        cmp_ans = 0;
        int m = cycle.size();

        FOR(j, 1, m) p[0][j] = p[0][j - 1] + cycle[j].second;
        for (int j = m - 2; ~j; j--) p[1][j] = p[1][j + 1] + cycle[j + 1].second;

        FOR(j, 0, m) {
            calc_tree(cycle[j].first);
            p[2][j] = dp[cycle[j].first] - p[0][j];
            p[3][j] = dp[cycle[j].first] + p[0][j];
            p[4][j] = dp[cycle[j].first] + p[1][j];
        }

        ll mx = p[2][0];
        FOR(j, 1, m) {
            cmp_ans = max(cmp_ans, p[3][j] + mx);
            mx = max(mx, p[2][j]);
        }
        mx = p[4][m - 1];
        for (int j = m - 2; ~j; j--) {
            cmp_ans = max(cmp_ans, p[3][j] + mx + discarded);
            mx = max(mx, p[4][j]);
        }

        ans += cmp_ans;

        FOR(j, 0, 5) FOR(k, 0, m) p[j][k] = 0;
    }
    cout << ans;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 18 ms 24064 KB Output is correct
2 Correct 18 ms 24192 KB Output is correct
3 Correct 18 ms 24192 KB Output is correct
4 Correct 19 ms 24064 KB Output is correct
5 Correct 19 ms 24064 KB Output is correct
6 Correct 19 ms 24064 KB Output is correct
7 Correct 19 ms 24064 KB Output is correct
8 Correct 18 ms 24168 KB Output is correct
9 Correct 18 ms 24064 KB Output is correct
10 Correct 19 ms 24192 KB Output is correct
11 Correct 19 ms 24192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 24320 KB Output is correct
2 Correct 19 ms 24192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 20 ms 24320 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 26 ms 25728 KB Output is correct
2 Correct 37 ms 29172 KB Output is correct
3 Correct 29 ms 25848 KB Output is correct
4 Correct 23 ms 24960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 30704 KB Output is correct
2 Correct 62 ms 33392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 111 ms 39792 KB Output is correct
2 Correct 117 ms 49000 KB Output is correct
3 Correct 144 ms 62952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 177 ms 51496 KB Output is correct
2 Correct 234 ms 85860 KB Output is correct
3 Correct 259 ms 97992 KB Output is correct
4 Correct 329 ms 131072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 380 ms 81196 KB Output is correct
2 Runtime error 618 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 437 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -