Submission #53720

# Submission time Handle Problem Language Result Execution time Memory
53720 2018-07-01T05:56:03 Z 강태규(#1435) Islands (IOI08_islands) C++11
100 / 100
1077 ms 76140 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_map>
#include <functional>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>

using namespace std;
typedef long long llong;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<llong, llong> pll;

const int MAXN = 1e6 + 1;
int n;
int nxt[MAXN];
int len[MAXN];
bool vis[MAXN];
bool fin[MAXN];
int st[MAXN];
llong dist[MAXN];
llong mx[MAXN];
llong mx2[MAXN];
llong mxs[MAXN];
pll dq[MAXN << 1];
int ord, it;
int tp[MAXN];

int dfs(int x) {
    int ret = 0;
    vis[x] = 1;
    if (!fin[nxt[x]]) {
        if (vis[nxt[x]]) {
            st[ord++] = x;
            ret = nxt[x];
        }
        else {
            ret = dfs(nxt[x]);
            if (ret == x) ret = 0;
            else if (ret == 0) dist[x] = dist[nxt[x]] + 1;
        }
    }
    else dist[x] = dist[nxt[x]] + 1;
    fin[x] = 1;
    return ret;
}

llong dfs2(int x, int s) {
    tp[it++] = x;
    if (nxt[x] == s) return dist[x] + len[x];
    dist[nxt[x]] = dist[x] + len[x];
    llong ret = dfs2(nxt[x], s);
    mxs[x] = max(mxs[x], mxs[nxt[x]]);
    return ret;
}

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d%d", nxt + i, len + i);
        tp[i - 1] = i;
    }
    for (int i = 1; i <= n; ++i) {
        if (!fin[i]) dfs(i);
    }
    sort(tp, tp + n, [&](int i, int j) { return dist[i] > dist[j]; });
    for (int i = 0; i < n; ++i) {
        int j = tp[i];
        llong v = mx[j] + len[j];
        mxs[j] = max(mxs[j], mx[j] + mx2[j]);
        if (dist[j]) {
            if (mx[nxt[j]] < v) {
                mx2[nxt[j]] = mx[nxt[j]];
                mx[nxt[j]] = v;
            }
            else if (mx2[nxt[j]] < v) {
                mx2[nxt[j]] = v;
            }
            mxs[nxt[j]] = max(mxs[nxt[j]], mxs[j]);
        }
    }
    llong ret = 0;
    for (int i = 0; i < ord; ++i) {
        it = 0;
        dist[st[i]] = 0;
        llong cy = dfs2(st[i], st[i]);
        llong ans = mxs[st[i]];
        int top = 0, bot = 0;
        for (int j = 1; j < it; ++j) {
            llong v = dist[tp[j]] + mx[tp[j]];
            while (bot < top && dq[top - 1].first <= v) --top;
            dq[top++] = pll(v, j);
        }
        for (int j = 0; j < it; ++j) {
            ans = max(ans, dq[bot].first + mx[tp[j]] - dist[tp[j]]);
            
            llong v = dist[tp[j]] + mx[tp[j]] + cy;
            if (bot < top && dq[bot].second == j + 1) ++bot;
            while (bot < top && dq[top - 1].first <= v) --top;
            dq[top++] = pll(v, j + it);
        }
        ret += ans;
    }
    printf("%lld\n", ret);
	return 0;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:65:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
islands.cpp:67:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", nxt + i, len + i);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 484 KB Output is correct
3 Correct 3 ms 484 KB Output is correct
4 Correct 2 ms 484 KB Output is correct
5 Correct 2 ms 596 KB Output is correct
6 Correct 3 ms 672 KB Output is correct
7 Correct 2 ms 748 KB Output is correct
8 Correct 2 ms 748 KB Output is correct
9 Correct 2 ms 748 KB Output is correct
10 Correct 2 ms 748 KB Output is correct
11 Correct 2 ms 748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 748 KB Output is correct
2 Correct 3 ms 748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 748 KB Output is correct
2 Correct 4 ms 876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 1304 KB Output is correct
2 Correct 17 ms 2668 KB Output is correct
3 Correct 16 ms 2668 KB Output is correct
4 Correct 7 ms 2668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 3436 KB Output is correct
2 Correct 37 ms 5228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 75 ms 10304 KB Output is correct
2 Correct 104 ms 11024 KB Output is correct
3 Correct 119 ms 16548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 187 ms 17848 KB Output is correct
2 Correct 180 ms 29420 KB Output is correct
3 Correct 210 ms 29420 KB Output is correct
4 Correct 283 ms 41444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 338 ms 41444 KB Output is correct
2 Correct 452 ms 53588 KB Output is correct
3 Correct 304 ms 53588 KB Output is correct
4 Correct 401 ms 55020 KB Output is correct
5 Correct 396 ms 55020 KB Output is correct
6 Correct 1077 ms 55020 KB Output is correct
7 Correct 467 ms 64268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 370 ms 76140 KB Output is correct
2 Correct 511 ms 76140 KB Output is correct
3 Correct 395 ms 76140 KB Output is correct
4 Correct 410 ms 76140 KB Output is correct
5 Correct 427 ms 76140 KB Output is correct
6 Correct 393 ms 76140 KB Output is correct
7 Correct 1012 ms 76140 KB Output is correct