Submission #503798

# Submission time Handle Problem Language Result Execution time Memory
503798 2022-01-08T22:57:11 Z tabr Islands (IOI08_islands) C++17
80 / 100
764 ms 131072 KB
#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

const int N = (int) 1e6;
int p[N];
int l[N];
int nxt[N];
long long dist[N];
long long max_dist[N];
bool cycle[N];
bool was[N];
vector<int> g[N];
pair<int, long long> deq[N];
int order[N];

int dsu_get(int x) {
    if (p[x] == x) {
        return x;
    } else {
        return p[x] = dsu_get(p[x]);
    }
}

bool dsu_unite(int x, int y) {
    x = dsu_get(x);
    y = dsu_get(y);
    if (x == y) {
        return false;
    }
    p[x] = y;
    return true;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    iota(p, p + n, 0);
    for (int i = 0; i < n; i++) {
        int j;
        cin >> j >> l[i];
        j--;
        g[j].emplace_back(i);
        dsu_unite(i, j);
        nxt[i] = j;
    }
    long long ans = 0;
    for (int i = 0; i < n; i++) {
        if (i != dsu_get(i)) {
            continue;
        }
        int c = i;
        while (!was[c]) {
            was[c] = 1;
            c = nxt[c];
        }
        int u = nxt[c];
        cycle[c] = 1;
        int sz = 1;
        while (u != c) {
            cycle[u] = 1;
            u = nxt[u];
            sz++;
        }
        int sz2 = 0;
        int beg = 0;
        int end = 0;
        int x = c;
        for (int jj = 0; jj < sz; jj++) {
            int j = x;
            x = nxt[x];
            deq[end++].first = j;
            while (beg < end) {
                int v = deq[beg++].first;
                order[sz2++] = v;
                for (int to : g[v]) {
                    if (cycle[to]) {
                        continue;
                    }
                    dist[to] = dist[v] + l[to];
                    deq[end++].first = to;
                }
            }
        }
        long long t = 0;
        for (int j = sz2 - 1; j >= 0; j--) {
            int v = order[j];
            max_dist[v] = max(max_dist[v], dist[v]);
            if (cycle[v]) {
                continue;
            }
            int to = nxt[v];
            t = max(t, max_dist[to] + max_dist[v] - dist[to] * 2);
            max_dist[to] = max(max_dist[to], max_dist[v]);
        }
        long long y = l[c];
        beg = end = 0;
        deq[end++] = make_pair(0, 0);
        x = nxt[c];
        for (int jj = 1; jj < sz * 2; jj++) {
            int j = jj % sz;
            if (deq[beg].first + sz == jj) {
                beg++;
            }
            t = max(t, y + max_dist[x] - deq[beg].second);
            long long z = y - max_dist[x];
            while (beg < end && deq[end - 1].second >= z) {
                end--;
            }
            y += l[x];
            deq[end++] = make_pair(j, z);
            x = nxt[x];
        }
        ans += t;
    }
    cout << ans << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23848 KB Output is correct
2 Correct 12 ms 23756 KB Output is correct
3 Correct 13 ms 23884 KB Output is correct
4 Correct 11 ms 23852 KB Output is correct
5 Correct 12 ms 23756 KB Output is correct
6 Correct 11 ms 23740 KB Output is correct
7 Correct 11 ms 23820 KB Output is correct
8 Correct 14 ms 23876 KB Output is correct
9 Correct 12 ms 23760 KB Output is correct
10 Correct 13 ms 23756 KB Output is correct
11 Correct 13 ms 23748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23908 KB Output is correct
2 Correct 12 ms 23916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23892 KB Output is correct
2 Correct 12 ms 24012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 24644 KB Output is correct
2 Correct 21 ms 26036 KB Output is correct
3 Correct 21 ms 25168 KB Output is correct
4 Correct 18 ms 24452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 27056 KB Output is correct
2 Correct 41 ms 29224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 36984 KB Output is correct
2 Correct 67 ms 39304 KB Output is correct
3 Correct 105 ms 41484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 106 ms 44044 KB Output is correct
2 Correct 140 ms 56180 KB Output is correct
3 Correct 147 ms 64936 KB Output is correct
4 Correct 232 ms 69268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 218 ms 74836 KB Output is correct
2 Correct 519 ms 83996 KB Output is correct
3 Correct 200 ms 62052 KB Output is correct
4 Correct 273 ms 80960 KB Output is correct
5 Correct 252 ms 82432 KB Output is correct
6 Correct 764 ms 84252 KB Output is correct
7 Runtime error 360 ms 131072 KB Execution killed with signal 11
# Verdict Execution time Memory Grader output
1 Correct 274 ms 114792 KB Output is correct
2 Correct 262 ms 102084 KB Output is correct
3 Runtime error 402 ms 131072 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -