Submission #503791

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

struct dsu {
    vector<int> p;
    vector<int> sz;
    int n;

    dsu(int _n) : n(_n) {
        p.resize(n);
        iota(p.begin(), p.end(), 0);
        sz.assign(n, 1);
    }

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

    inline bool unite(int x, int y) {
        x = get(x);
        y = get(y);
        if (x == y) {
            return false;
        }
        if (sz[x] > sz[y]) {
            swap(x, y);
        }
        p[x] = y;
        sz[y] += sz[x];
        return true;
    }

    inline bool same(int x, int y) {
        return (get(x) == get(y));
    }
};

const int N = (int) 1e6;
int l[N];
int nxt[N];
vector<int> vs[N];
long long dist[N];
long long max_dist[N];
bool cycle[N];
bool was[N];
vector<int> g[N];
int p[N];
int que[N];
pair<int, long long> deq[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;
    }
    for (int i = 0; i < n; i++) {
        vs[dsu_get(i)].emplace_back(i);
    }
    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;
        vector<int> x(1, c);
        while (u != c) {
            cycle[u] = 1;
            x.emplace_back(u);
            u = nxt[u];
        }
        vector<int> order;
        int beg = 0;
        int end = 0;
        for (int j : x) {
            que[end++] = j;
            while (beg < end) {
                int v = que[beg++];
                order.emplace_back(v);
                for (int to : g[v]) {
                    if (cycle[to]) {
                        continue;
                    }
                    dist[to] = dist[v] + l[to];
                    que[end++] = to;
                }
            }
        }
        long long t = 0;
        reverse(order.begin(), order.end());
        for (int v : order) {
            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]);
        }
        beg = end = 0;
        deq[end++] = make_pair(0, 0);
        long long y = l[c];
        int sz = (int) x.size();
        for (int jj = 1; jj < sz * 2; jj++) {
            int j = jj % sz;
            if (x[deq[beg].first] == x[j]) {
                beg++;
            }
            t = max(t, y + max_dist[x[j]] - deq[beg].second);
            long long z = y - max_dist[x[j]];
            while (beg < end && deq[end - 1].second >= z) {
                end--;
            }
            y += l[x[j]];
            deq[end++] = make_pair(j, z);
        }
        ans += t;
    }
    cout << ans << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 21 ms 47308 KB Output is correct
2 Correct 22 ms 47348 KB Output is correct
3 Correct 23 ms 47316 KB Output is correct
4 Correct 26 ms 47256 KB Output is correct
5 Correct 22 ms 47320 KB Output is correct
6 Correct 26 ms 47252 KB Output is correct
7 Correct 23 ms 47344 KB Output is correct
8 Correct 21 ms 47308 KB Output is correct
9 Correct 21 ms 47220 KB Output is correct
10 Correct 22 ms 47344 KB Output is correct
11 Correct 22 ms 47236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 47308 KB Output is correct
2 Correct 25 ms 47432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 47436 KB Output is correct
2 Correct 25 ms 47580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 48280 KB Output is correct
2 Correct 33 ms 49588 KB Output is correct
3 Correct 29 ms 48696 KB Output is correct
4 Correct 25 ms 47992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 50616 KB Output is correct
2 Correct 50 ms 53336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 60324 KB Output is correct
2 Correct 80 ms 64296 KB Output is correct
3 Correct 99 ms 67644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 131 ms 69488 KB Output is correct
2 Correct 152 ms 78868 KB Output is correct
3 Correct 175 ms 94372 KB Output is correct
4 Correct 206 ms 103464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 248 ms 90488 KB Output is correct
2 Correct 486 ms 117508 KB Output is correct
3 Correct 231 ms 89344 KB Output is correct
4 Correct 284 ms 107844 KB Output is correct
5 Correct 297 ms 106620 KB Output is correct
6 Correct 874 ms 107952 KB Output is correct
7 Correct 310 ms 124852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 313 ms 130460 KB Output is correct
2 Correct 299 ms 117888 KB Output is correct
3 Runtime error 298 ms 131072 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -