답안 #331533

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
331533 2020-11-28T20:17:33 Z quindecim Islands (IOI08_islands) C++14
0 / 100
89 ms 131076 KB
#pragma GCC optimize ("Ofast")
#pragma GCC target ("avx2")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9+7;
const int MX = 1e7+10;
const ll INF = 1ll<<61;

int n, m, r, a[MX], w[MX];
vector<int> adj[MX];
ll ans, ret;
vector<ll> V, S;
bitset<MX> vv;

inline int cycle(int u) {
    if(vv[u]) return u;
    vv[u] = 1;
    r = cycle(a[u]);
    if(r) {
        V.push_back(u);
        S.push_back(w[u]);
        if(r==u) return 0;
        else return r;
    }
    vv[u] = 0;
    return 0;
}

inline ll dfs(int u) {
    ll mx = 0;
    vv[u] = 1;
    for(int &v : adj[u]) {
        if(!vv[v]) {
            ll r = dfs(v) + w[v];
            ret = max(ret, mx+r);
            mx = max(mx, r);
        }
    }
    return mx;
}

inline void solve(int u) {
    V.clear(); S.clear();
    cycle(u);
    reverse(V.begin(), V.end());
    reverse(S.begin(), S.end());
    for(auto &v : V) v = dfs(v);
    m = V.size();
    for(int i = m-1; i > 0; --i) swap(S[i], S[i-1]);
    for(int i = 1; i < m; ++i) S[i] += S[i-1];
    ll m1 = -INF, m2 = -INF;
    for(int i = 0; i < m; ++i) {
        ret = max(ret, max(m1+S[i]+V[i], m2-S[i]+V[i]+S[m-1]));
        m1 = max(m1, V[i]-S[i]);
        m2 = max(m2, V[i]+S[i]);
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin >> n;
    for(int i = 1; i <= n; ++i) {
        cin >> a[i] >> w[i];
        adj[a[i]].push_back(i);
    }
    for(int i = 1; i <= n; ++i)
        if(!vv[i]) solve(i), ans+=ret, ret = 0;
    cout << ans << "\n";
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Runtime error 74 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Runtime error 76 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Runtime error 74 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
4 Runtime error 74 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
5 Runtime error 74 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
6 Runtime error 74 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
7 Runtime error 73 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
8 Runtime error 72 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
9 Runtime error 88 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
10 Runtime error 73 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
11 Runtime error 76 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
# 결과 실행 시간 메모리 Grader output
1 Runtime error 73 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 73 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 89 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 84 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 74 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 71 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 72 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 73 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -