답안 #114471

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
114471 2019-06-01T12:31:18 Z popovicirobert Islands (IOI08_islands) C++14
80 / 100
620 ms 131072 KB
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44

using namespace std;

const ll INF = 1e18;

struct Edge {
    int nod, cst;
    int id;
};

vector < vector <Edge> > g;
vector <int> vis;
vector < pair <int, int> > cyc;

vector < pair <int, int> > stk;
vector <int> nodes;

void get_cyc(int nod, int id) {
    nodes.push_back(nod);
    vis[nod] = 1;
    for(auto it : g[nod]) {
        if(vis[it.nod] == 0) {
            stk.push_back({nod, it.cst});
            get_cyc(it.nod, it.id);
            stk.pop_back();
        }
        else if(vis[it.nod] == 1 && it.id != id) {
            int i = (int) stk.size() - 1;
            while(i >= 0 && stk[i].first != it.nod) {
                cyc.push_back(stk[i]);
                i--;
            }
            cyc.push_back(stk[i]);
            reverse(cyc.begin(), cyc.end());
            cyc.push_back({nod, it.cst});
        }
    }
    vis[nod] = 2;
}

vector <ll> dp1, dp2;

void dfs(int nod, ll &ans)  {
    vis[nod] = 1;
    for(auto it : g[nod]) {
        if(vis[it.nod] == 0) {
            dfs(it.nod, ans);
            ll cur = dp1[it.nod] + it.cst;
            if(dp1[nod] <= cur) {
                dp2[nod] = dp1[nod];
                dp1[nod] = cur;
            }
            else if(dp2[nod] <= cur) {
                dp2[nod] = cur;
            }
        }
    }
    ans = max(ans, dp1[nod] + dp2[nod]);
}


inline ll solve(int nod) {

    cyc.clear(), nodes.clear();
    get_cyc(nod, 0);

    for(auto it : nodes) {
        vis[it] = 0;
    }
    ll sum = 0;
    for(auto it : cyc) {
        vis[it.first] = 1;
        sum += it.second;
    }

    ll cur = 0, mx1 = -INF, mx2 = -INF, ans = 0;
    for(auto it : cyc) {
        dfs(it.first, ans);
        if(it.first != cyc[0].first) {
            ans = max(ans, max(mx1 + dp1[it.first] + cur, mx2 + dp1[it.first] - cur));
        }
        mx1 = max(mx1, dp1[it.first] - cur);
        mx2 = max(mx2, dp1[it.first] + cur + sum);
        cur += it.second;
    }

    return ans;

}

int main() {
    //ifstream cin("A.in");
    //ofstream cout("A.out");
    int i, n;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> n;

    g.resize(n + 1);
    for(i = 1; i <= n; i++) {
        int nod, cst;
        cin >> nod >> cst;
        g[i].push_back({nod, cst, i});
        g[nod].push_back({i, cst, i});
    }

    vis.resize(n + 1);
    dp1.resize(n + 1), dp2.resize(n + 1);

    ll ans = 0;
    for(i = 1; i <= n; i++) {
        if(vis[i] == 0) {
            ans += solve(i);
        }
    }

    cout << ans;

    //cin.close();
    //cout.close();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 4 ms 1024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 1920 KB Output is correct
2 Correct 30 ms 6016 KB Output is correct
3 Correct 24 ms 2816 KB Output is correct
4 Correct 8 ms 1536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 7328 KB Output is correct
2 Correct 57 ms 10924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 100 ms 20784 KB Output is correct
2 Correct 107 ms 26716 KB Output is correct
3 Correct 134 ms 41052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 151 ms 35416 KB Output is correct
2 Correct 219 ms 75080 KB Output is correct
3 Correct 247 ms 81000 KB Output is correct
4 Correct 339 ms 112192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 364 ms 94480 KB Output is correct
2 Runtime error 620 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 382 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -