답안 #503752

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

const int N = (int) 1e6 + 10;
vector<int> g[N];
int l[N];
int e[N];
int was[N];
int pv[N];
int cycle[N];
long long d1[N];
long long d2[N];
int c1;
int c2;
int sz;
int dead;
long long t;

void dfs1(int v, int pe) {
    was[v] = 1;
    for (auto id : g[v]) {
        if (id == pe) {
            continue;
        }
        int to = v ^ id ^ e[id];
        if (was[to]) {
            if (c1 != -1) {
                continue;
            }
            dead = id;
            c1 = v;
            c2 = to;
            cycle[v]++;
            if (pv[to] != -1) {
                cycle[pv[to]]--;
            }
            continue;
        }
        pv[to] = v;
        dfs1(to, id);
        cycle[v] += cycle[to];
    }
    sz += cycle[v];
}

void dfs2(int v, int p, int cnt, long long now) {
    cnt += cycle[v];
    d1[cnt] = max(d1[cnt], now);
    for (auto id : g[v]) {
        int to = v ^ id ^ e[id];
        if (to == p || id == dead) {
            continue;
        }
        dfs2(to, v, cnt, now + l[id]);
    }
}

long long dfs3(int v, int p, long long now) {
    vector<long long> c;
    for (auto id : g[v]) {
        int to = v ^ id ^ e[id];
        if (to == p || id == dead) {
            continue;
        }
        c.emplace_back(dfs3(to, v, now + l[id]));
    }
    t = max(t, now);
    if (c.empty()) {
        return now;
    }
    sort(c.rbegin(), c.rend());
    if (c.size() > 1) {
        t = max(t, c[0] + c[1] - 2 * now);
    }
    return c[0];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        int j;
        cin >> j >> l[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]);
        }
    }
    long long ans = 0;
    for (int i = 0; i < n; i++) {
        if (was[i]) {
            continue;
        }
        c1 = -1, c2 = -1;
        sz = 0;
        dead = -1;
        pv[i] = -1;
        dfs1(i, -1);
        for (int j = 0; j <= sz; j++) {
            d1[j] = d2[j] = 0;
        }
        dfs2(c1, c2, 0, 0);
        swap(d1, d2);
        dfs2(c2, c1, 0, 0);
        swap(d1, d2);
        for (int j = 0; j < sz; j++) {
            d1[j + 1] = max(d1[j + 1], d1[j]);
            d2[j + 1] = max(d2[j + 1], d2[j]);
        }
        t = 0;
        dfs3(c1, c2, 0);
        dfs3(c2, c1, 0);
        for (int j = 1; j < sz; j++) {
            t = max(t, d1[j] + d2[sz - j] + l[dead]);
        }
        ans += t;
    }
    cout << ans << '\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 39372 KB Output is correct
2 Correct 27 ms 39436 KB Output is correct
3 Correct 21 ms 39512 KB Output is correct
4 Correct 23 ms 39384 KB Output is correct
5 Correct 26 ms 39396 KB Output is correct
6 Correct 33 ms 39440 KB Output is correct
7 Correct 56 ms 39372 KB Output is correct
8 Correct 31 ms 39500 KB Output is correct
9 Correct 95 ms 39476 KB Output is correct
10 Correct 22 ms 39376 KB Output is correct
11 Correct 270 ms 39480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 39484 KB Output is correct
2 Execution timed out 2044 ms 39552 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Correct 1159 ms 39500 KB Output is correct
2 Correct 24 ms 39884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 40432 KB Output is correct
2 Correct 48 ms 43112 KB Output is correct
3 Correct 48 ms 40652 KB Output is correct
4 Correct 49 ms 39964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 44644 KB Output is correct
2 Correct 87 ms 46720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 142 ms 52632 KB Output is correct
2 Correct 148 ms 59372 KB Output is correct
3 Correct 194 ms 71480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 347 ms 61872 KB Output is correct
2 Correct 298 ms 93052 KB Output is correct
3 Correct 403 ms 98504 KB Output is correct
4 Correct 357 ms 121840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 430 ms 98652 KB Output is correct
2 Runtime error 836 ms 131076 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 331 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -