Submission #503784

# Submission time Handle Problem Language Result Execution time Memory
503784 2022-01-08T22:16:21 Z tabr Islands (IOI08_islands) C++17
67 / 100
2000 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));
    }
};

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);
    dsu uf(n);
    for (int i = 0; i < n; i++) {
        int j;
        cin >> j >> l[i];
        j--;
        uf.unite(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]);
        }
    }
    vector<vector<int>> vs(n);
    for (int i = 0; i < n; i++) {
        vs[uf.get(i)].emplace_back(i);
    }
    long long ans = 0;
    vector<long long> mx(n);
    vector<int> cycle(n);
    vector<int> was(n);
    for (int i = 0; i < n; i++) {
        if (i != uf.get(i)) {
            continue;
        }
        int c = i;
        while (!was[c]) {
            was[c] = 1;
            c = e[c];
        }
        cycle[c] = 1;
        int u = e[c];
        while (u != c) {
            cycle[u] = 1;
            u = e[u];
        }
        queue<int> que;
        for (int j : vs[i]) {
            if (g[j].size() == 1) {
                que.emplace(j);
            }
        }
        long long t = 0;
        while (!que.empty()) {
            int v = que.front();
            que.pop();
            int to = e[v];
            t = max(t, mx[to] + mx[v] + l[v]);
            mx[to] = max(mx[to], mx[v] + l[v]);
            if (!cycle[to]) {
                que.emplace(to);
            }
        }
        vector<int> x;
        x.emplace_back(c);
        u = e[c];
        while (u != c) {
            x.emplace_back(u);
            u = e[u];
        }
        u = e[c];
        x.emplace_back(c);
        while (u != c) {
            x.emplace_back(u);
            u = e[u];
        }
        deque<pair<int, long long>> deq;
        deq.emplace_back(0, 0);
        long long y = l[c];
        for (int j = 1; j < (int) x.size(); j++) {
            if (x[deq.front().first] == x[j]) {
                deq.pop_front();
            }
            t = max(t, y + mx[x[j]] - deq.front().second);
            long long z = y - mx[x[j]];
            while (!deq.empty() && deq.back().second >= z) {
                deq.pop_back();
            }
            y += l[x[j]];
            deq.emplace_back(j, z);
        }
        ans += t;
    }
    cout << ans << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Incorrect 0 ms 332 KB Output isn't correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 2 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 2124 KB Output is correct
2 Correct 13 ms 4552 KB Output is correct
3 Correct 16 ms 2768 KB Output is correct
4 Incorrect 6 ms 1432 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 6348 KB Output is correct
2 Correct 33 ms 12120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 76 ms 23716 KB Output is correct
2 Correct 102 ms 28296 KB Output is correct
3 Correct 92 ms 32344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 126 ms 46316 KB Output is correct
2 Correct 162 ms 61096 KB Output is correct
3 Correct 194 ms 74752 KB Output is correct
4 Correct 219 ms 82100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 293 ms 121860 KB Output is correct
2 Correct 555 ms 131072 KB Output is correct
3 Correct 275 ms 102800 KB Output is correct
4 Correct 341 ms 131072 KB Output is correct
5 Correct 354 ms 131072 KB Output is correct
6 Execution timed out 2078 ms 122856 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 329 ms 113840 KB Output is correct
2 Incorrect 338 ms 113888 KB Output isn't correct
3 Halted 0 ms 0 KB -