Submission #197478

# Submission time Handle Problem Language Result Execution time Memory
197478 2020-01-21T12:18:03 Z SamAnd Islands (IOI08_islands) C++17
90 / 100
1197 ms 131076 KB
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
const int N = 1000006;
const long long INF = 1000000009000000009;
struct ban
{
    int x;
    long long d;
    ban(){}
    ban(int x, long long d)
    {
        this->x = x;
        this->d = d;
    }
};

int n;
int uu[N], dd[N];
vector<ban> a[N];

//vector<vector<int> > vv;

int c[N];

int p[N];
int s, f;
bool dfs(int x)
{
    c[x] = 1;
    int h = uu[x];
    if (c[h] == 1)
    {
        s = h;
        f = x;
        c[x] = 2;
        return true;
    }
    if (c[h] == 0)
    {
        p[h] = x;
        if (dfs(h))
        {
            c[x] = 2;
            return true;
        }
    }
    c[x] = 2;
    return false;
}

long long yans;

long long dp[N];
void dfs1(int x, int p)
{
    long long max1 = 0, max2 = 0;
    for (int i = 0; i < a[x].size(); ++i)
    {
        ban h = a[x][i];
        if (h.x == p || c[h.x] == 3)
            continue;
        dfs1(h.x, x);
        dp[x] = max(dp[x], h.d + dp[h.x]);
        if (h.d + dp[h.x] >= max1)
        {
            max2 = max1;
            max1 = h.d + dp[h.x];
        }
        else if (h.d + dp[h.x] >= max2)
            max2 = h.d + dp[h.x];
    }
    yans = max(yans, max1 + max2);
    yans = max(yans, dp[x]);
}

long long pp[N], ss[N];
void solvv(const vector<int>& v)
{
    if (v.size() <= 1)
        return;
    long long maxu = dp[v[0]];
    for (int i = 1; i < v.size(); ++i)
    {
        int d = dd[v[i - 1]];
        maxu += d;
        yans = max(yans, maxu + dp[v[i]]);
        maxu = max(maxu, dp[v[i]]);
    }
    long long u = 0;
    pp[0] = dp[v[0]];
    for (int i = 1; i < v.size(); ++i)
    {
        int d = dd[v[i - 1]];
        u += d;
        pp[i] = max(pp[i - 1], u + dp[v[i]]);
    }
    u = 0;
    ss[v.size() - 1] = dp[v.back()];
    for (int i = v.size() - 2; i >= 0; --i)
    {
        int d = dd[v[i]];
        u += d;
        ss[i] = max(ss[i + 1], u + dp[v[i]]);
    }
    for (int i = 0; i < v.size() - 1; ++i)
    {
        yans = max(yans, pp[i] + ss[i + 1] + dd[v.back()]);
    }
}

int main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    #endif // SOMETHING
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i)
    {
        int x, d;
        scanf("%d%d", &x, &d);
        uu[i] = x;
        dd[i] = d;
        a[i].push_back(ban(x, d));
        a[x].push_back(ban(i, d));
    }
    long long ans = 0;
    for (int i = 1; i <= n; ++i)
    {
        if (!c[i])
        {
            if (dfs(i))
            {
                vector<int> v;
                for (int x = f; x != s; x = p[x])
                    v.push_back(x);
                v.push_back(s);
                reverse(v.begin(), v.end());
                //vv.push_back(v);
                yans = 0;
                for (int i = 0; i < v.size(); ++i)
                    c[v[i]] = 3;
                for (int i = 0; i < v.size(); ++i)
                    dfs1(v[i], v[i]);
                solvv(v);
                ans += yans;
            }
        }
    }
    /*for (int i = 0; i < vv.size(); ++i)
    {
        for (int j = 0; j < vv[i].size(); ++j)
            printf("%d ", vv[i][j]);
        printf("\n");
    }*/
    /*memset(c, 0, sizeof c);
    for (int i = 0; i < vv.size(); ++i)
    {
        yans = 0;
        for (int j = 0; j < vv[i].size(); ++j)
            c[vv[i][j]] = 1;
        for (int j = 0; j < vv[i].size(); ++j)
            dfs1(vv[i][j], vv[i][j]);
        solvv(vv[i]);
        ans += yans;
    }*/
    printf("%lld\n", ans);
    return 0;
}

Compilation message

islands.cpp: In function 'void dfs1(int, int)':
islands.cpp:58:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < a[x].size(); ++i)
                     ~~^~~~~~~~~~~~~
islands.cpp: In function 'void solvv(const std::vector<int>&)':
islands.cpp:83:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 1; i < v.size(); ++i)
                     ~~^~~~~~~~~~
islands.cpp:92:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 1; i < v.size(); ++i)
                     ~~^~~~~~~~~~
islands.cpp:106:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < v.size() - 1; ++i)
                     ~~^~~~~~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:141:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 for (int i = 0; i < v.size(); ++i)
                                 ~~^~~~~~~~~~
islands.cpp:143:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 for (int i = 0; i < v.size(); ++i)
                                 ~~^~~~~~~~~~
islands.cpp:117:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
islands.cpp:121:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &x, &d);
         ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23928 KB Output is correct
2 Correct 25 ms 23928 KB Output is correct
3 Correct 25 ms 23928 KB Output is correct
4 Correct 25 ms 23800 KB Output is correct
5 Correct 24 ms 23804 KB Output is correct
6 Correct 23 ms 23928 KB Output is correct
7 Correct 24 ms 23804 KB Output is correct
8 Correct 25 ms 23804 KB Output is correct
9 Correct 24 ms 23864 KB Output is correct
10 Correct 24 ms 23800 KB Output is correct
11 Correct 24 ms 23800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 23928 KB Output is correct
2 Correct 26 ms 24076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 24096 KB Output is correct
2 Correct 32 ms 24184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 25080 KB Output is correct
2 Correct 60 ms 27164 KB Output is correct
3 Correct 46 ms 25464 KB Output is correct
4 Correct 36 ms 24696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 28448 KB Output is correct
2 Correct 74 ms 31752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 132 ms 39484 KB Output is correct
2 Correct 132 ms 41828 KB Output is correct
3 Correct 164 ms 48492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 222 ms 54008 KB Output is correct
2 Correct 276 ms 68972 KB Output is correct
3 Correct 287 ms 72228 KB Output is correct
4 Correct 371 ms 90984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 428 ms 88128 KB Output is correct
2 Correct 913 ms 120156 KB Output is correct
3 Correct 425 ms 90892 KB Output is correct
4 Correct 536 ms 116316 KB Output is correct
5 Correct 523 ms 116640 KB Output is correct
6 Correct 1197 ms 103944 KB Output is correct
7 Correct 565 ms 126660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 522 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -