답안 #687551

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
687551 2023-01-26T14:43:52 Z finn__ Islands (IOI08_islands) C++17
9 / 100
1085 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(it != 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 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Runtime error 1 ms 468 KB Execution killed with signal 6
3 Runtime error 1 ms 468 KB Execution killed with signal 6
4 Correct 1 ms 212 KB Output is correct
5 Runtime error 1 ms 332 KB Execution killed with signal 6
6 Runtime error 1 ms 340 KB Execution killed with signal 6
7 Runtime error 1 ms 468 KB Execution killed with signal 6
8 Runtime error 1 ms 468 KB Execution killed with signal 6
9 Runtime error 1 ms 464 KB Execution killed with signal 6
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 596 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 596 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 12 ms 2332 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 41 ms 10508 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 145 ms 28868 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 266 ms 51576 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 720 ms 63188 KB Output is correct
2 Runtime error 1085 ms 131072 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 757 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -