답안 #687552

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

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

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

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

    return {0, UINT_MAX};
}

// 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], 0U));
        assert(it != g[c[i]].end());
        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], 0U));
        assert(jt != g[c[i]].end());
        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] + prefix_sum + dp);
        dp = max(dp, (int64_t)root_paths[i % m] - (int64_t)prefix_sum);
        prefix_sum += pre_dis[i % 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());
        sort(g[i].begin(), g[i].end());
    }

    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 Incorrect 0 ms 212 KB Output isn't correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Incorrect 1 ms 340 KB Output isn't correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Incorrect 0 ms 212 KB Output isn't correct
7 Incorrect 0 ms 212 KB Output isn't correct
8 Incorrect 1 ms 212 KB Output isn't correct
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 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 12 ms 1364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 5196 KB Output is correct
2 Incorrect 70 ms 8004 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 162 ms 14316 KB Output is correct
2 Correct 154 ms 20564 KB Output is correct
3 Incorrect 199 ms 30324 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 278 ms 25548 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 715 ms 63148 KB Output is correct
2 Incorrect 1106 ms 110492 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 741 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -