답안 #687539

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
687539 2023-01-26T14:07:40 Z finn__ Islands (IOI08_islands) C++17
15 / 100
1133 ms 131072 KB
#include <bits/stdc++.h>
using namespace std;

vector<vector<pair<unsigned, unsigned>>> g;

bool find_cycle(unsigned u, vector<unsigned> &c, vector<bool> &vis, unsigned p = -1)
{
    if (vis[u])
        return 1;

    vis[u] = 1;
    for (auto const &[v, w] : g[u])
        if (v != p)
            if (find_cycle(v, c, vis, u))
            {
                c.push_back(u);
                return 1;
            }

    return 0;
}

// Returns (longest path inside subtree, longest path to root)
pair<uint64_t, uint64_t> longest_tree_path(unsigned u, unsigned p = -1)
{
    pair<uint64_t, uint64_t> ans = {0, 0};
    for (auto const &[v, w] : g[u])
    {
        if (v != p)
        {
            auto const [sl, rl] = longest_tree_path(v, u);
            ans.first = max(ans.first, max(sl, ans.second + rl + w));
            ans.second = max(ans.second, rl + w);
        }
    }
    return ans;
}

uint64_t longest_simple_path(unsigned u, vector<bool> &cvis)
{
    vector<unsigned> c;
    find_cycle(u, c, cvis);

    if (c.empty())
        return longest_tree_path(u).first;

    size_t const m = c.size();
    vector<uint64_t> root_paths(c.size());
    uint64_t ans = 0;
    vector<unsigned> pre_dis(c.size()), next_dis(c.size());

    for (size_t i = 0; i < m; i++)
    {
        auto it = lower_bound(g[c[i]].begin(), g[c[i]].end(), make_pair(c[(i + 1) % m], UINT_MAX), greater<pair<unsigned, unsigned>>());
        next_dis[i] = it->second;
        g[c[i]].erase(it);

        auto jt = lower_bound(g[c[i]].begin(), g[c[i]].end(), make_pair(c[(i - 1 + m) % m], UINT_MAX), greater<pair<unsigned, unsigned>>());
        pre_dis[i] = jt->second;
        g[c[i]].erase(jt);
    }

    for (size_t i = 0; i < m; i++)
    {
        auto const [sl, rl] = longest_tree_path(c[i]);
        ans = max(ans, sl);
        root_paths[i] = rl;
    }

    int64_t dp = 0, prefix_sum = 0;
    for (size_t i = 0; i < m; i++)
    {
        ans = max(ans, root_paths[i] + prefix_sum + dp);
        dp = max(dp, (int64_t)root_paths[i] - (int64_t)prefix_sum);
        prefix_sum += next_dis[i];
    }

    dp = 0, prefix_sum = 0;

    for (size_t i = m; i; i--)
    {
        ans = max(ans, root_paths[(i + m) % m] + prefix_sum + dp);
        dp = max(dp, (int64_t)root_paths[(i + m) % m] - (int64_t)prefix_sum);
        prefix_sum += pre_dis[(i + m) % m];
    }

    return ans;
}

void mark_component(unsigned u, vector<bool> &vis)
{
    vis[u] = 1;
    for (auto const &[v, w] : g[u])
        if (!vis[v])
            mark_component(v, vis);
}

bool edge_cmp(pair<unsigned, unsigned> const &a, pair<unsigned, unsigned> const &b)
{
    return a.first == b.first;
}

int main()
{
    size_t n;
    cin >> n;

    g = vector<vector<pair<unsigned, unsigned>>>(n);
    for (size_t i = 0; i < n; i++)
    {
        unsigned u, w;
        cin >> u >> w;
        g[i].emplace_back(u - 1, w);
        g[u - 1].emplace_back(i, w);
    }

    for (unsigned i = 0; i < n; i++)
    {
        sort(g[i].begin(), g[i].end(), greater<pair<unsigned, unsigned>>());
        g[i].resize(unique(g[i].begin(), g[i].end(), edge_cmp) - g[i].begin());
    }

    vector<bool> vis(n, 0), cvis(n, 0);
    uint64_t max_distance = 0;
    for (unsigned i = 0; i < n; i++)
    {
        if (!vis[i])
        {
            mark_component(i, vis);
            max_distance += longest_simple_path(i, cvis);
        }
    }

    cout << max_distance << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Incorrect 1 ms 340 KB Output isn't correct
4 Correct 1 ms 212 KB Output is correct
5 Incorrect 0 ms 212 KB Output isn't correct
6 Correct 1 ms 212 KB Output is correct
7 Incorrect 0 ms 212 KB Output isn't correct
8 Runtime error 62 ms 131072 KB Execution killed with signal 9
9 Incorrect 0 ms 212 KB Output isn't correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 596 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 66 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 137 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 45 ms 10544 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 170 ms 30728 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 301 ms 25260 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 751 ms 78612 KB Output is correct
2 Incorrect 1133 ms 117980 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 801 ms 131072 KB Output is correct
2 Correct 820 ms 103320 KB Output is correct
3 Runtime error 817 ms 131072 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -