답안 #555534

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
555534 2022-05-01T07:20:53 Z alextodoran Islands (IOI08_islands) C++17
90 / 100
1202 ms 131072 KB
/**
 ____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N_MAX = 1000000;

int N;
struct Edge {
    int to;
    int len;
};
vector <Edge> adj[N_MAX + 2];

bool seen[N_MAX + 2];
Edge par[N_MAX + 2];
int depth[N_MAX + 2];

tuple <int, int, int> backEdge;

int curr[N_MAX + 2];
void dfs (int u) {
    while (u != 0) {
        seen[u] = true;
        if (curr[u] < (int) adj[u].size()) {
            Edge e = adj[u][curr[u]++];
            if (seen[e.to] == false) {
                par[e.to] = Edge{u, e.len};
                depth[e.to] = depth[u] + 1;
                u = e.to;
            } else if (depth[e.to] > depth[u]) {
                backEdge = make_tuple(u, e.to, e.len);
            }
        } else {
            u = par[u].to;
        }
    }
}

bool root[N_MAX + 2];

ll maxAny[N_MAX + 2];
ll maxDown[N_MAX + 2];

queue <int> q;
vector <int> order;
void compute (int s) {
    q.push(s);
    seen[s] = true; depth[s] = 0;
    while (q.empty() == false) {
        int u = q.front(); q.pop();
        order.push_back(u);
        for (Edge e : adj[u]) {
            if (seen[e.to] == false && root[e.to] == false) {
                q.push(e.to);
                seen[e.to] = true;
                depth[e.to] = depth[u] + 1;
            }
        }
    }
    reverse(order.begin(), order.end());
    for (int u : order) {
        vector <ll> maxDowns;
        for (Edge e : adj[u]) {
            if (depth[u] < depth[e.to] && root[e.to] == false) {
                maxAny[u] = max(maxAny[u], maxAny[e.to]);
                maxDown[u] = max(maxDown[u], maxDown[e.to] + e.len);
                maxDowns.push_back(maxDown[e.to] + e.len);
            }
        }
        sort(maxDowns.begin(), maxDowns.end(), greater <ll> ());
        if ((int) maxDowns.size() >= 2) {
            maxAny[u] = max(maxAny[u], maxDowns[0] + maxDowns[1]);
        }
        maxAny[u] = max(maxAny[u], maxDown[u]);
    }
    order.clear();
}

vector <vector <int>> rootGroups;

multiset <ll> s;

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> N;
    for (int u = 1; u <= N; u++) {
        Edge e;
        cin >> e.to >> e.len;
        assert(u != e.to);
        adj[u].push_back(Edge{e.to, e.len});
        adj[e.to].push_back(Edge{u, e.len});
    }

    for (int u = 1; u <= N; u++) {
        if (seen[u] == false) {
            dfs(u);
            int v1, v2, len; tie(v1, v2, len) = backEdge;
            swap(v1, v2);
            par[v2] = Edge{v1, len};
            rootGroups.push_back({});
            int v = v1;
            while (v != v2) {
                root[v] = true;
                rootGroups.back().push_back(v);
                v = par[v].to;
            }
            root[v] = true;
            rootGroups.back().push_back(v);
        }
    }

    fill(seen + 1, seen + N + 1, false);
    for (int u = 1; u <= N; u++) {
        if (root[u] == true) {
            compute(u);
        }
    }

    ll answer = 0;
    for (vector <int> &roots : rootGroups) {
        ll totalLen = 0;
        ll maxLen = 0;
        for (int u : roots) {
            totalLen += par[u].len;
            maxLen = max(maxLen, maxAny[u]);
        }

        ll len = 0;
        for (int i = (int) roots.size() - 1; i > 0; i--) {
            len += par[roots[i]].len;
            s.insert(maxDown[roots[i]] + len);
        }
        ll lazy = 0;

        for (int i = 0, u = roots[0]; i < (int) roots.size(); i++) {
            assert(s.empty() == false);
            maxLen = max(maxLen, *s.rbegin() + lazy + maxDown[u]);
            s.insert(maxDown[u] - lazy);
            lazy += par[u].len;
            u = par[u].to;
            assert(s.find((maxDown[u] + totalLen) - lazy) != s.end());
            s.erase(s.find((maxDown[u] + totalLen) - lazy));
        }
        answer += maxLen;
        s.clear();
    }
    cout << answer << "\n";

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 13 ms 23892 KB Output is correct
4 Correct 12 ms 23796 KB Output is correct
5 Correct 12 ms 23836 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 13 ms 23740 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 12 ms 23764 KB Output is correct
10 Correct 12 ms 23764 KB Output is correct
11 Correct 15 ms 23764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23952 KB Output is correct
2 Correct 12 ms 23892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 24020 KB Output is correct
2 Correct 14 ms 24168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 25044 KB Output is correct
2 Correct 33 ms 27136 KB Output is correct
3 Correct 26 ms 25376 KB Output is correct
4 Correct 18 ms 24532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 28408 KB Output is correct
2 Correct 60 ms 31648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 100 ms 38796 KB Output is correct
2 Correct 159 ms 41880 KB Output is correct
3 Correct 185 ms 49092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 187 ms 52348 KB Output is correct
2 Correct 259 ms 69500 KB Output is correct
3 Correct 466 ms 72944 KB Output is correct
4 Correct 468 ms 88524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 388 ms 111576 KB Output is correct
2 Correct 932 ms 115568 KB Output is correct
3 Correct 348 ms 81480 KB Output is correct
4 Correct 505 ms 109892 KB Output is correct
5 Correct 568 ms 110676 KB Output is correct
6 Correct 1202 ms 93320 KB Output is correct
7 Correct 722 ms 126000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 361 ms 91328 KB Output is correct
2 Correct 370 ms 91452 KB Output is correct
3 Runtime error 596 ms 131072 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -