답안 #503810

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
503810 2022-01-08T23:35:04 Z tabr Islands (IOI08_islands) C++17
80 / 100
925 ms 118568 KB
#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

const int N = (int) 1e6;
vector<int> g[N];
int deg[N + 1];
int p[N];
int l[N];
int nxt[N];
long long dist[N];
long long max_dist[N];
bool cycle[N];
bool was[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);
        deg[j + 1]++;
        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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23776 KB Output is correct
2 Correct 12 ms 23812 KB Output is correct
3 Correct 12 ms 23920 KB Output is correct
4 Correct 15 ms 23768 KB Output is correct
5 Correct 12 ms 23872 KB Output is correct
6 Correct 16 ms 23788 KB Output is correct
7 Correct 12 ms 23868 KB Output is correct
8 Correct 13 ms 23840 KB Output is correct
9 Correct 17 ms 23876 KB Output is correct
10 Correct 16 ms 23808 KB Output is correct
11 Correct 15 ms 23756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23940 KB Output is correct
2 Correct 12 ms 23884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23900 KB Output is correct
2 Correct 15 ms 24116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 24736 KB Output is correct
2 Correct 25 ms 26252 KB Output is correct
3 Correct 28 ms 25244 KB Output is correct
4 Correct 17 ms 24508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 27296 KB Output is correct
2 Correct 41 ms 29652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 37780 KB Output is correct
2 Correct 72 ms 40256 KB Output is correct
3 Correct 86 ms 42428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 117 ms 45544 KB Output is correct
2 Correct 163 ms 58024 KB Output is correct
3 Correct 188 ms 67180 KB Output is correct
4 Correct 197 ms 71776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 257 ms 74776 KB Output is correct
2 Correct 567 ms 85832 KB Output is correct
3 Correct 243 ms 65112 KB Output is correct
4 Correct 302 ms 84724 KB Output is correct
5 Correct 273 ms 86416 KB Output is correct
6 Correct 925 ms 87860 KB Output is correct
7 Incorrect 314 ms 95072 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 300 ms 118568 KB Output is correct
2 Correct 414 ms 105912 KB Output is correct
3 Incorrect 300 ms 112716 KB Output isn't correct
4 Halted 0 ms 0 KB -