답안 #791634

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
791634 2023-07-24T08:24:05 Z phoebe Islands (IOI08_islands) C++17
90 / 100
685 ms 131072 KB
#include <bits/stdc++.h>
using namespace std;

// #define int long long
#define ll long long
#define pii pair<ll, int>
#define F first
#define S second
#define PB push_back
#define ALL(x) x.begin(), x.end()
#define FOR(i, n) for (int i = 0; i < n; i++)
#define NYOOM ios::sync_with_stdio(0); cin.tie(0);
#define endl '\n'
const int INF = 1e9 + 7;
const ll LLINF = 1ll<<60;

const int maxn = 1e6 + 10;
int n, p[maxn], l[maxn];
short seen[maxn] = {0}; // 0 = unseen, 1 = seeing, 2 = seen
ll val[maxn], cur = 0, re = 0; // val[v][i] = node v connected to i nodes already
// val[v][2] >= val[v][1] >= val[v][0]
bool on_cycle[maxn] = {0};
vector<int> adj[maxn];

void dfs(int v, int p){
    val[v] = 0;
    ll best = -1, second_best = -1;
    for (auto u : adj[v]){
        if (on_cycle[u] || u == p) continue;
        dfs(u, v);
        val[v] = max(val[v], val[u] + l[u]);
        if (best == -1) best = val[u] + l[u];
        else second_best = max(second_best, val[u] + l[u]);
        if (second_best > best) swap(best, second_best);
    }
    cur = max(cur, best + second_best);
}

void solve(int s, int sz){
    int v = s;
    // forward
    deque<pii> dq; // {val, idx}
    ll sum = 0;
    FOR(i, sz * 2){
        while (!dq.empty() && dq.front().S <= i - sz) dq.pop_front();
        if (!dq.empty()) cur = max(cur, dq.front().F + sum + val[v]);
        while (!dq.empty() && dq.back().F + sum <= val[v]) dq.pop_back();
        dq.push_back({val[v] - sum, i});
        sum += l[v];
        v = p[v];
    }
}

void find_cycle(int s){
    int v = s; 
    while (seen[v] == 0){ // while unseen
        seen[v] = 1; v = p[v];
    }
    if (seen[v] == 1){ // found a cycle
        cur = 0; int sz = 0;
        while (!on_cycle[v]){
            sz++; on_cycle[v] = true; v = p[v];
        }
        FOR(i, sz){
            dfs(v, v); v = p[v];
        }
        solve(v, sz);
        re += cur;
    }
    v = s; 
    while (seen[v] != 2){
        seen[v] = 2; v = p[v];
    }
}

signed main(){
    NYOOM;
    // freopen("isl.in", "r", stdin);
    // freopen("isl.out", "w", stdout);
    cin >> n;
    for (int i = 1; i <= n; i++){
        cin >> p[i] >> l[i];
        adj[p[i]].PB(i);
    }
    for (int i = 1; i <= n; i++){
        if (seen[i] != 2) find_cycle(i);
    }
    cout << re;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23764 KB Output is correct
2 Correct 11 ms 23836 KB Output is correct
3 Correct 10 ms 23756 KB Output is correct
4 Correct 9 ms 23744 KB Output is correct
5 Correct 9 ms 23764 KB Output is correct
6 Correct 10 ms 23764 KB Output is correct
7 Correct 9 ms 23764 KB Output is correct
8 Correct 10 ms 23764 KB Output is correct
9 Correct 9 ms 23764 KB Output is correct
10 Correct 13 ms 23776 KB Output is correct
11 Correct 12 ms 23864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23892 KB Output is correct
2 Correct 11 ms 23868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23952 KB Output is correct
2 Correct 17 ms 23956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 24660 KB Output is correct
2 Correct 22 ms 25504 KB Output is correct
3 Correct 17 ms 24848 KB Output is correct
4 Correct 19 ms 24276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 26396 KB Output is correct
2 Correct 31 ms 29148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 33532 KB Output is correct
2 Correct 60 ms 38300 KB Output is correct
3 Correct 70 ms 39304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 92 ms 45284 KB Output is correct
2 Correct 113 ms 46980 KB Output is correct
3 Correct 114 ms 63052 KB Output is correct
4 Correct 153 ms 63716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 216 ms 60968 KB Output is correct
2 Correct 423 ms 79372 KB Output is correct
3 Correct 253 ms 64316 KB Output is correct
4 Correct 280 ms 74600 KB Output is correct
5 Correct 238 ms 71204 KB Output is correct
6 Correct 685 ms 75624 KB Output is correct
7 Correct 248 ms 85660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 208 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -