답안 #503788

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
503788 2022-01-08T22:26:31 Z tabr Islands (IOI08_islands) C++17
67 / 100
2000 ms 61272 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];
int deg[N];
vector<int> vs[N];
long long mx[N];
bool cycle[N];
bool was[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--;
        uf.unite(i, j);
        nxt[i] = j;
        deg[i]++;
        deg[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];
        }
        for (int j : vs[i]) {
            if (deg[j] == 1) {
                que.emplace(j);
            }
        }
        long long t = 0;
        while (!que.empty()) {
            int v = que.front();
            que.pop();
            int to = nxt[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);
            }
        }
        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 + 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);
        }
        deq.clear();
        ans += t;
    }
    cout << ans << '\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23756 KB Output is correct
2 Incorrect 12 ms 23860 KB Output isn't correct
3 Correct 12 ms 23756 KB Output is correct
4 Correct 11 ms 23756 KB Output is correct
5 Correct 12 ms 23848 KB Output is correct
6 Correct 12 ms 23840 KB Output is correct
7 Correct 12 ms 23848 KB Output is correct
8 Correct 12 ms 23756 KB Output is correct
9 Correct 12 ms 23784 KB Output is correct
10 Correct 15 ms 23756 KB Output is correct
11 Correct 14 ms 23856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23872 KB Output is correct
2 Correct 12 ms 23884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23884 KB Output is correct
2 Correct 12 ms 23908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 24396 KB Output is correct
2 Correct 26 ms 25084 KB Output is correct
3 Correct 25 ms 24620 KB Output is correct
4 Incorrect 16 ms 24140 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 25744 KB Output is correct
2 Correct 39 ms 27292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 30996 KB Output is correct
2 Correct 55 ms 31668 KB Output is correct
3 Correct 70 ms 35208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 37060 KB Output is correct
2 Correct 112 ms 42252 KB Output is correct
3 Correct 119 ms 44968 KB Output is correct
4 Correct 152 ms 53012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 222 ms 51384 KB Output is correct
2 Correct 248 ms 60576 KB Output is correct
3 Correct 177 ms 50616 KB Output is correct
4 Correct 211 ms 59180 KB Output is correct
5 Correct 211 ms 61272 KB Output is correct
6 Execution timed out 2082 ms 56408 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 221 ms 56116 KB Output is correct
2 Incorrect 224 ms 55192 KB Output isn't correct
3 Halted 0 ms 0 KB -