Submission #114673

# Submission time Handle Problem Language Result Execution time Memory
114673 2019-06-02T09:25:41 Z zubec Islands (IOI08_islands) C++14
44 / 100
461 ms 131072 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;

const int N = 1000100;

int dsu[N], sz[N];

int n;

ll ans = 0;

int len[N], nxt[N];

vector <pair<int, int> > g[N], gg[N];

bool used[N], inCycle[N];

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

int cId;

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

    for (int i = 0; i < g[v].size(); i++){
        int to = gg[v][i].first, id = gg[v][i].second;
        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 to = g[v][i].first;
        if (to == p)
            continue;
        curmx = max(curmx, dfs(to, v)+g[v][i].second);
    }
    return curmx;
}

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

    cin >> n;
    for (int i = 1; i <= n; i++){
        int to;
        cin >> to >> len[i];
        nxt[i] = to;
        g[i].push_back({to, len[i]});
        gg[i].push_back({to, i});
        g[to].push_back({i, len[i]});
        gg[to].push_back({i, 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]);
        for (int v = cEnd; v != cStrt; v = pr[v]){
            cId = pr2[cId];
            cycle.push_back(v);
            lens.push_back(len[cId]);
        }
        for (int j = 0; j < cycle.size(); j++){
            inCycle[cycle[j]] = 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 to = g[v][j].first;
                if (inCycle[to])
                    continue;
                curmx = max(curmx, dfs(to, v)+g[v][j].second);
            }



            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:27: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:54: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:92:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j = 0; j < cycle.size(); j++){
                         ~~^~~~~~~~~~~~~~
islands.cpp:95:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j = 1; j < lens.size(); j++){
                         ~~^~~~~~~~~~~~~
islands.cpp:103:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j = 0; j < cycle.size(); j++){
                         ~~^~~~~~~~~~~~~~
islands.cpp:108: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 41 ms 47356 KB Output is correct
2 Incorrect 41 ms 47352 KB Output isn't correct
3 Correct 42 ms 47480 KB Output is correct
4 Correct 40 ms 47352 KB Output is correct
5 Correct 42 ms 47352 KB Output is correct
6 Incorrect 41 ms 47360 KB Output isn't correct
7 Correct 39 ms 47352 KB Output is correct
8 Incorrect 42 ms 47360 KB Output isn't correct
9 Correct 41 ms 47352 KB Output is correct
10 Incorrect 41 ms 47352 KB Output isn't correct
11 Correct 45 ms 47356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 47608 KB Output is correct
2 Correct 44 ms 47468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 43 ms 47480 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 50 ms 48928 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 83 ms 53848 KB Output is correct
2 Correct 89 ms 58132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 166 ms 67956 KB Output is correct
2 Correct 166 ms 72460 KB Output is correct
3 Correct 206 ms 83760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 247 ms 85576 KB Output is correct
2 Incorrect 319 ms 112352 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 379 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 461 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -