답안 #561617

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
561617 2022-05-13T08:54:57 Z piOOE 도로 폐쇄 (APIO21_roads) C++17
0 / 100
73 ms 22616 KB
#include "roads.h"

#include <bits/stdc++.h>

using namespace std;

#define all(x) begin(x), end(x)

typedef long long ll;

const int N = 100001;
int A[N], B[N], W[N], n;
ll dp[N][2];
int k;
vector<pair<int, int>> g[N];

void dfs(int v, int p) {
    vector<pair<int, int>> adj;
    for (auto [to, w]: g[v]) {
        if (to != p) {
            dfs(to, v);
            adj.emplace_back(to, w);
        }
    }
    if (k > (int) size(adj)) {
        vector<ll> nw;
        ll ans = 0;
        for (auto [to, w]: adj) {
            ans += dp[to][0] + w;
            nw.push_back(dp[to][1] - dp[to][0] - w);
        }
        sort(all(nw));
        for (int i = 0; i < (int) size(nw); ++i) {
            if (nw[i] < 0)
                ans += nw[i];
        }
        dp[v][1] = dp[0][v] = ans;
    } else {
        vector<ll> nw;
        ll ans = 0;
        for (auto [to, w]: adj) {
            ans += dp[to][0] + w;
            nw.push_back(dp[to][1] - dp[to][0] - w);
        }
        sort(all(nw));
        ll added = 0;
        for (int i = 0; i < k - 1; ++i) {
            if (nw[i] < 0)
                added += nw[i];
        }
        dp[v][1] = ans + added;
        added = 0;
        for (int i = 0; i < k; ++i) {
            added += nw[i];
        }
        dp[v][0] = min(dp[v][1], ans + added);
    }
}

vector<ll> minimum_closure_costs(int nn, vector<int> uu, vector<int> vv, vector<int> ww) {
    n = nn;
    for (int i = 0; i < n - 1; ++i) {
        A[i] = uu[i], B[i] = vv[i], W[i] = ww[i];
        g[A[i]].emplace_back(B[i], W[i]);
        g[B[i]].emplace_back(A[i], W[i]);
    }
    if (n == 1) {
        assert(false);
    }
    if (n == 2) {
        assert(false);
    }
    vector<ll> ans;
    dfs(0, -1);
    k = 0;
    ans.push_back(dp[0][0]);
    assert(dp[0][0] == accumulate(all(W), 0ll));
    k = 1;
    ans.push_back(dp[0][0]);
    if (n > 2) {
        assert(false);
    }
    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4 ms 5204 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4 ms 5204 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 3 ms 5204 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 3 ms 5204 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 73 ms 22616 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 73 ms 22616 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4 ms 5204 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -