Submission #114721

# Submission time Handle Problem Language Result Execution time Memory
114721 2019-06-02T12:40:37 Z zubec Islands (IOI08_islands) C++14
90 / 100
1740 ms 131072 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;

const int N = 1000100;

int n;

ll ans = 0;

int len[N], nxt[N];

vector <int> g[N];

bool used[N], block[N];

int used2[N], pr[N], cEnd, cStrt, pr2[N];

int cId;

int getTo(int v, int id){
    return (v == id ? nxt[v] : id);
}

int fnd_cycle(int v, int p){
    used2[v] = 1;

    for (int i = 0; i < g[v].size(); i++){
        int id = g[v][i];
        int to = getTo(v, id);
        if (id == p)
            continue;
        if (used2[to] == 0){
            pr[to] = v;
            pr2[to] = id;
            if (fnd_cycle(to, id)){
                return 1;
            }
        } else
        if (used2[to] == 1){
            cEnd = v;
            cId = id;
            cStrt = to;
            return 1;
        }
    }

    used2[v] = 2;

    return 0;
}

ll dfs(int v, int p){
    used[v] = 1;
    ll curmx = 0;
    for (int i = 0; i < g[v].size(); i++){
        int id = g[v][i];
        int to = getTo(v, id);
        if (to == p)
            continue;
        curmx = max(curmx, dfs(to, v)+len[id]);
    }
    return curmx;
}

ll mx, mxver;

void dfs2(int v, int p, ll curlen){
    if (curlen > mx){
        mx = curlen;
        mxver = v;
    }
    for (int i = 0; i < g[v].size(); i++){
        int id = g[v][i];
        int to = getTo(v, id);
        if (!block[id] && to != p)
            dfs2(to, v, curlen+len[id]);
    }
}

ll fnd_diam(int v){
    mx = -1, mxver = 0;
    dfs2(v, 0, 0);
    mx = -1;
    dfs2(mxver, 0, 0);
    return mx;
}

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);

    cin >> n;
    for (int i = 1; i <= n; i++){
        cin >> nxt[i] >> len[i];
        g[i].push_back(i);
        g[nxt[i]].push_back(i);
    }
    for (int i = 1; i <= n; i++){
        if (used[i])
            continue;

        fnd_cycle(i, 0);


        vector <int> cycle;
        vector <ll> lens;
        cycle.push_back(cStrt);
        lens.push_back(0);
        lens.push_back(len[cId]);
        block[cId] = 1;
        for (int v = cEnd; v != cStrt; v = pr[v]){
            cId = pr2[v];
            cycle.push_back(v);
            lens.push_back(len[cId]);
            block[cId] = 1;
        }
        for (int j = 1; j < lens.size(); j++){
            lens[j] += lens[j-1];
        }

        ll curans = 0;

        ll mx1 = -1e18, mx2 = -1e18;


        for (int j = 0; j < cycle.size(); j++){
            int v = cycle[j];
            used[v] = 1;

            ll curmx = 0;
            for (int j = 0; j < g[v].size(); j++){
                int id = g[v][j];
                int to = getTo(v, id);
                if (block[id])
                    continue;
                curmx = max(curmx, dfs(to, v)+len[id]);
            }

            curans = max(curans, curmx);
            curans = max(curans, fnd_diam(v));

            if (j == 0){

                mx1 = max(mx1, curmx);
                mx2 = max(mx2, curmx);

            } else {

                curans = max(curans, curmx+lens[j] + mx1 );
                curans = max(curans, curmx+lens.back()-lens[j] + mx2 );

                mx1 = max(mx1, curmx-lens[j]);
                mx2 = max(mx2, curmx+lens[j]);

            }

        }

        ans += curans;

    }
    cout << ans;


}

Compilation message

islands.cpp: In function 'int fnd_cycle(int, int)':
islands.cpp:29:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[v].size(); i++){
                     ~~^~~~~~~~~~~~~
islands.cpp: In function 'll dfs(int, int)':
islands.cpp:57:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[v].size(); i++){
                     ~~^~~~~~~~~~~~~
islands.cpp: In function 'void dfs2(int, int, ll)':
islands.cpp:74:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[v].size(); i++){
                     ~~^~~~~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:118:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j = 1; j < lens.size(); j++){
                         ~~^~~~~~~~~~~~~
islands.cpp:127:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j = 0; j < cycle.size(); j++){
                         ~~^~~~~~~~~~~~~~
islands.cpp:132:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (int j = 0; j < g[v].size(); j++){
                             ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 22 ms 23928 KB Output is correct
2 Correct 21 ms 23936 KB Output is correct
3 Correct 22 ms 23920 KB Output is correct
4 Correct 21 ms 23808 KB Output is correct
5 Correct 22 ms 23824 KB Output is correct
6 Correct 21 ms 23808 KB Output is correct
7 Correct 21 ms 23928 KB Output is correct
8 Correct 21 ms 23936 KB Output is correct
9 Correct 23 ms 23812 KB Output is correct
10 Correct 61 ms 23928 KB Output is correct
11 Correct 24 ms 23936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 23936 KB Output is correct
2 Correct 24 ms 23936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 24064 KB Output is correct
2 Correct 69 ms 24312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 24952 KB Output is correct
2 Correct 45 ms 27000 KB Output is correct
3 Correct 34 ms 25208 KB Output is correct
4 Correct 29 ms 24576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 28316 KB Output is correct
2 Correct 58 ms 30868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 109 ms 36500 KB Output is correct
2 Correct 117 ms 40964 KB Output is correct
3 Correct 151 ms 47564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 163 ms 46540 KB Output is correct
2 Correct 223 ms 63768 KB Output is correct
3 Correct 252 ms 71560 KB Output is correct
4 Correct 302 ms 83200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 378 ms 76304 KB Output is correct
2 Correct 859 ms 103196 KB Output is correct
3 Correct 312 ms 67768 KB Output is correct
4 Correct 398 ms 94772 KB Output is correct
5 Correct 390 ms 94776 KB Output is correct
6 Correct 1740 ms 75768 KB Output is correct
7 Correct 433 ms 118372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 416 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -