Submission #503771

# Submission time Handle Problem Language Result Execution time Memory
503771 2022-01-08T21:30:40 Z tabr Islands (IOI08_islands) C++17
0 / 100
2000 ms 130248 KB
#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<vector<int>> g(n);
    vector<int> l(n);
    vector<int> e(n);
    for (int i = 0; i < n; i++) {
        int j;
        cin >> j >> l[i];
        j--;
        e[i] = j;
        g[i].emplace_back(i);
        g[j].emplace_back(i);
    }
    for (int i = 0; i < n; i++) {
        if (e[e[i]] == i) {
            l[e[i]] = l[i] = max(l[e[i]], l[i]);
        }
    }
    long long ans = 0;
    vector<int> was(n);
    vector<int> pv(n);
    vector<int> cycle(n);
    vector<int> last_id(n);
    vector<vector<long long>> c(n);
    for (int i = 0; i < n; i++) {
        if (was[i]) {
            continue;
        }
        int c1 = -1, c2 = -1;
        int sz = 0;
        int dead = -1;
        pv[i] = -1;
        {  // dfs1
            stack<pair<int, int>> stk;
            stk.emplace(i, -1);
            while (!stk.empty()) {
                auto [v, pe] = stk.top();
                was[v] = 1;
                debug(v);
                if (last_id[v] == (int) g[v].size()) {
                    for (auto id : g[v]) {
                        int to = v ^ id ^ e[id];
                        if (to == pv[v]) {
                            continue;
                        }
                        cycle[v] += cycle[to];
                    }
                    if (v == c1 || v == c2) {
                        cycle[v] = 1;
                    }
                    sz += cycle[v];
                    stk.pop();
                    continue;
                }
                int &gid = last_id[v];
                while (gid < (int) g[v].size()) {
                    int id = g[v][gid++];
                    if (id == pe) {
                        continue;
                    }
                    int to = v ^ id ^ e[id];
                    if (was[to]) {
                        debug(v, to);
                        if (c1 != -1) {
                            continue;
                        }
                        dead = id;
                        c1 = v;
                        c2 = to;
                        cycle[v]++;
                        if (pv[to] != -1) {
                            cycle[pv[to]]--;
                        }
                        continue;
                    }
                    pv[to] = v;
                    stk.emplace(to, id);
                    break;
                }
            }
        }
        vector<long long> d1(sz + 1), d2(sz + 1);
        for (int z = 0; z < 2; z++) {
            queue<tuple<int, int, int, long long>> que;
            que.emplace(c1, c2, 0, 0);
            while (!que.empty()) {
                auto [v, p, cnt, now] = que.front();
                que.pop();
                cnt += cycle[v];
                d1[cnt] = max(d1[cnt], now);
                for (auto id : g[v]) {
                    int to = v ^ id ^ e[id];
                    if (to == p || id == dead) {
                        continue;
                    }
                    que.emplace(to, v, cnt, now + l[id]);
                }
            };
            // dfs2(c1, c2, 0, 0);
            swap(c1, c2);
            swap(d1, d2);
        }
        for (int j = 0; j < sz; j++) {
            d1[j + 1] = max(d1[j + 1], d1[j]);
            d2[j + 1] = max(d2[j + 1], d2[j]);
        }
        long long t = 0;
        for (int z = 0; z < 2; z++) {
            stack<tuple<int, int, long long>> stk;
            stk.emplace(c1, c2, 0);
            while (!stk.empty()) {
                auto [v, p, now] = stk.top();
                t = max(t, now);
                if (last_id[v] == 0) {
                    stk.pop();
                    if (c[v].empty()) {
                        if (p != c2) {
                            c[p].emplace_back(now);
                        }
                        continue;
                    }
                    sort(c[v].rbegin(), c[v].rend());
                    if (c[v].size() > 1) {
                        t = max(t, c[v][0] + c[v][1] - 2 * now);
                    }
                    long long res = c[v][0];
                    c[v].clear();
                    if (p != c2) {
                        c[p].emplace_back(res);
                    }
                    continue;
                }
                int &gid = last_id[v];
                while (gid >= 0) {
                    gid--;
                    int id = g[v][gid];
                    int to = v ^ id ^ e[id];
                    if (to == p || id == dead) {
                        continue;
                    }
                    stk.emplace(to, v, now + l[id]);
                    c[v].emplace_back();
                }
            };
            swap(c1, c2);
        }
        debug(t);
        debug(d1);
        debug(d2);
        for (int j = 1; j < sz; j++) {
            t = max(t, d1[j] + d2[sz - j] + l[dead]);
        }
        ans += t;
    }
    cout << ans << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Execution timed out 2076 ms 204 KB Time limit exceeded
2 Runtime error 1 ms 460 KB Execution killed with signal 11
3 Runtime error 1 ms 460 KB Execution killed with signal 11
4 Execution timed out 2089 ms 204 KB Time limit exceeded
5 Execution timed out 2088 ms 204 KB Time limit exceeded
6 Execution timed out 2088 ms 204 KB Time limit exceeded
7 Execution timed out 2098 ms 204 KB Time limit exceeded
8 Runtime error 1 ms 460 KB Execution killed with signal 11
9 Execution timed out 2088 ms 204 KB Time limit exceeded
10 Execution timed out 2089 ms 204 KB Time limit exceeded
11 Execution timed out 2083 ms 204 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 588 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2078 ms 404 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 6 ms 3532 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2087 ms 5836 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2090 ms 21312 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2086 ms 41472 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2059 ms 130248 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2086 ms 110032 KB Time limit exceeded
2 Halted 0 ms 0 KB -