답안 #503789

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
503789 2022-01-08T22:35:08 Z tabr Islands (IOI08_islands) C++17
90 / 100
1027 ms 131076 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 + 10;
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 main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    dsu uf(n);
    for (int i = 0; i < n; i++) {
        int j;
        cin >> j >> l[i];
        j--;
        g[j].emplace_back(i);
        uf.unite(i, j);
        nxt[i] = j;
    }
    for (int i = 0; i < n; i++) {
        vs[uf.get(i)].emplace_back(i);
    }
    long long ans = 0;
    queue<int> que;
    deque<pair<int, long long>> deq;
    for (int i = 0; i < n; i++) {
        if (i != uf.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;
        for (int j : x) {
            que.emplace(j);
            while (!que.empty()) {
                int v = que.front();
                order.emplace_back(v);
                que.pop();
                for (int to : g[v]) {
                    if (cycle[to]) {
                        continue;
                    }
                    dist[to] = dist[v] + l[to];
                    que.emplace(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]);
        }
        deq.emplace_back(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.front().first] == x[j]) {
                deq.pop_front();
            }
            t = max(t, y + max_dist[x[j]] - deq.front().second);
            long long z = y - max_dist[x[j]];
            while (!deq.empty() && deq.back().second >= z) {
                deq.pop_back();
            }
            y += l[x[j]];
            deq.emplace_back(j, z);
        }
        deq.clear();
        ans += t;
    }
    cout << ans << '\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 47300 KB Output is correct
2 Correct 33 ms 47272 KB Output is correct
3 Correct 30 ms 47324 KB Output is correct
4 Correct 25 ms 47324 KB Output is correct
5 Correct 29 ms 47316 KB Output is correct
6 Correct 25 ms 47268 KB Output is correct
7 Correct 24 ms 47236 KB Output is correct
8 Correct 23 ms 47300 KB Output is correct
9 Correct 23 ms 47296 KB Output is correct
10 Correct 24 ms 47284 KB Output is correct
11 Correct 24 ms 47296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 47300 KB Output is correct
2 Correct 26 ms 47436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 47460 KB Output is correct
2 Correct 26 ms 47520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 48436 KB Output is correct
2 Correct 36 ms 49668 KB Output is correct
3 Correct 41 ms 48844 KB Output is correct
4 Correct 31 ms 48156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 50452 KB Output is correct
2 Correct 64 ms 53512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 95 ms 59524 KB Output is correct
2 Correct 98 ms 63424 KB Output is correct
3 Correct 114 ms 65496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 135 ms 70844 KB Output is correct
2 Correct 166 ms 79348 KB Output is correct
3 Correct 180 ms 90384 KB Output is correct
4 Correct 231 ms 93908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 296 ms 94780 KB Output is correct
2 Correct 552 ms 108636 KB Output is correct
3 Correct 268 ms 92172 KB Output is correct
4 Correct 341 ms 107236 KB Output is correct
5 Correct 311 ms 107260 KB Output is correct
6 Correct 1027 ms 109008 KB Output is correct
7 Correct 339 ms 131072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 345 ms 119132 KB Output is correct
2 Correct 308 ms 118168 KB Output is correct
3 Runtime error 367 ms 131076 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -