Submission #56542

# Submission time Handle Problem Language Result Execution time Memory
56542 2018-07-11T15:13:00 Z admin Islands (IOI08_islands) C++14
100 / 100
950 ms 104264 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
 
const int N = 1000010; 
const ll INF = 1e18;
ll par[N], len[N], deg[N], h1[N], h2[N], dp[N];
 
int main()
{
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        int v, w;
        scanf("%d%d", &v, &w);
        par[i] = v;
        len[i] = w;
        ++deg[v];
    }
    queue<int> Q;
    for (int i = 1; i <= n; ++i) {
        if (deg[i] == 0)
            Q.push(i);
    }
    while (!Q.empty()) {
        int u = Q.front(); Q.pop();
        int v = par[u];
        ll w = len[u];
        if (h1[u]+w > h1[v]) {
            h2[v] = h1[v];
            h1[v] = h1[u]+w;
        } else if (h1[u]+w > h2[v]) {
            h2[v] = h1[u]+w;
        }
        dp[u] = max(dp[u], h1[u]+h2[u]);
        dp[v] = max(dp[v], dp[u]);
        if (--deg[v] == 0)
            Q.push(v);
    }
    ll ttans = 0;
    for (int i = 1; i <= n; ++i) {
        if (deg[i] == 1) {
            int u = i;
            vector<int> cyc;
            ll ttd = 0;
            do {
                cyc.push_back(u);
                deg[u] = 0;
                ttd += len[u];
                u = par[u];
            } while (u != i);
 
            ll mxm = -INF, mxp = -INF, d = 0, ans = -INF;
            for (auto u : cyc) {
                dp[u] = max(dp[u], h1[u]+h2[u]);
                ans = max(ans, dp[u]);
                ans = max(ans, h1[u]+d+mxm);
                ans = max(ans, h1[u]-d+ttd+mxp);
                mxm = max(mxm, h1[u]-d);
                mxp = max(mxp, h1[u]+d);
                d += len[u];
            }
            ttans += ans;
        }
    }
    printf("%lld\n", ttans);
 
    return 0;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:12:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
islands.cpp:15:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &v, &w);
         ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 376 KB Output is correct
2 Correct 3 ms 568 KB Output is correct
3 Correct 3 ms 568 KB Output is correct
4 Correct 2 ms 568 KB Output is correct
5 Correct 2 ms 568 KB Output is correct
6 Correct 2 ms 636 KB Output is correct
7 Correct 2 ms 636 KB Output is correct
8 Correct 3 ms 656 KB Output is correct
9 Correct 2 ms 656 KB Output is correct
10 Correct 3 ms 656 KB Output is correct
11 Correct 2 ms 696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 696 KB Output is correct
2 Correct 4 ms 696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 740 KB Output is correct
2 Correct 4 ms 740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 1380 KB Output is correct
2 Correct 15 ms 2404 KB Output is correct
3 Correct 13 ms 2404 KB Output is correct
4 Correct 5 ms 2404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 3172 KB Output is correct
2 Correct 35 ms 5092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 10348 KB Output is correct
2 Correct 93 ms 10348 KB Output is correct
3 Correct 108 ms 13772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 135 ms 18684 KB Output is correct
2 Correct 192 ms 26144 KB Output is correct
3 Correct 144 ms 26144 KB Output is correct
4 Correct 248 ms 33520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 362 ms 33520 KB Output is correct
2 Correct 512 ms 39956 KB Output is correct
3 Correct 315 ms 39956 KB Output is correct
4 Correct 340 ms 50848 KB Output is correct
5 Correct 348 ms 51756 KB Output is correct
6 Correct 697 ms 51756 KB Output is correct
7 Correct 318 ms 53360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 354 ms 53360 KB Output is correct
2 Correct 402 ms 53360 KB Output is correct
3 Correct 373 ms 53360 KB Output is correct
4 Correct 457 ms 53360 KB Output is correct
5 Correct 424 ms 79044 KB Output is correct
6 Correct 407 ms 90260 KB Output is correct
7 Correct 950 ms 104264 KB Output is correct