Submission #53711

# Submission time Handle Problem Language Result Execution time Memory
53711 2018-07-01T05:39:00 Z 강태규(#1435) Islands (IOI08_islands) C++11
64 / 100
450 ms 80560 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];
int vis[MAXN];
int fin[MAXN];
int st[MAXN];
llong dist[MAXN];
llong mx[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];
    return dfs2(nxt[x], s);
}

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 && dist[tp[i]]; ++i) {
        int j = tp[i];
        mx[nxt[j]] = max(mx[nxt[j]], mx[j] + len[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 = 0;
        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:61:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
islands.cpp:63: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 2 ms 376 KB Output is correct
2 Incorrect 2 ms 484 KB Output isn't correct
3 Correct 3 ms 560 KB Output is correct
4 Correct 2 ms 560 KB Output is correct
5 Correct 2 ms 560 KB Output is correct
6 Correct 2 ms 560 KB Output is correct
7 Correct 2 ms 560 KB Output is correct
8 Correct 2 ms 560 KB Output is correct
9 Correct 3 ms 560 KB Output is correct
10 Incorrect 2 ms 560 KB Output isn't correct
11 Correct 3 ms 560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 660 KB Output is correct
2 Correct 3 ms 660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 660 KB Output is correct
2 Correct 5 ms 848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 1256 KB Output is correct
2 Correct 20 ms 2280 KB Output is correct
3 Correct 14 ms 2280 KB Output is correct
4 Incorrect 7 ms 2280 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 3052 KB Output is correct
2 Correct 35 ms 4460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 8300 KB Output is correct
2 Correct 84 ms 9964 KB Output is correct
3 Correct 104 ms 17744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 166 ms 18248 KB Output is correct
2 Correct 194 ms 28276 KB Output is correct
3 Correct 172 ms 35064 KB Output is correct
4 Correct 310 ms 49216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 386 ms 49216 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 374 ms 80560 KB Output is correct
2 Incorrect 450 ms 80560 KB Output isn't correct
3 Halted 0 ms 0 KB -