Submission #1072911

#TimeUsernameProblemLanguageResultExecution timeMemory
1072911stdfloatRoad Closures (APIO21_roads)C++17
31 / 100
2093 ms33744 KiB
#include <bits/stdc++.h>
#include "roads.h"
using namespace std;
 
using ll = long long;
 
#define ff  first
#define ss  second
#define pii pair<int, int>
 
vector<bool> vis;

vector<vector<ll>> dp;
 
vector<vector<pii>> E;
 
void dfs(int x, int k, bool f, int p = -1) {
    vis[x] = true;

    if (dp[x][f] == LLONG_MAX) dp[x][f] = 0;
    else return;
 
    vector<ll> v;
    for (auto [i, w] : E[x]) {
        if (i == p) {
            if (f == 1) dp[x][f] += w;
            continue;
        }
 
        if ((int)E[i].size() >= k) {
            dfs(i, k, 0, x);
            dfs(i, k, 1, x);

            dp[x][f] += dp[i][0];
            v.push_back(dp[i][1] - dp[i][0]);
        }
        else v.push_back(w);
    }
 
    sort(v.begin(), v.end());
 
    for (int i = 0; i < (int)E[x].size() - k - f || (i < (int)v.size() && v[i] < 0); i++) {
        dp[x][f] += v[i];
    }

    // cout << "x " << x << ' ' << k << ' ' << f << ' ' << dp[x][f] << endl;
}
 
vector<ll> minimum_closure_costs(int n, vector<int> U, vector<int> V, vector<int> W) {
    E.assign(n, {});
 
    vector<ll> ans = {0};
    for (int i = 0; i < n - 1; i++) {
        ans[0] += W[i];
        E[U[i]].push_back({V[i], W[i]});
        E[V[i]].push_back({U[i], W[i]});
    }
 
    vector<pii> v;
    for (int i = 0; i < n; i++) {
        v.push_back({(int)E[i].size(), i});
    }

    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());

    vis.assign(n, false);
    dp.assign(n, vector<ll>(2, LLONG_MAX));
    for (int i = 1; i < n; i++) {
        while (v.back().ff < i) v.pop_back();

        for (auto j : v) {
            vis[j.ss] = false;
            dp[j.ss][0] = dp[j.ss][1] = LLONG_MAX;
        }

        // cout << "\ni " << i << '\n';

        ll sm = 0;
        for (auto j : v) {
            if (vis[j.ss]) continue;

            // cout << "\nj " << j.ss << ' ' << j.ff << ' ' << i << endl;

            dfs(j.ss, i, 0);
            sm += dp[j.ss][0];

            // cout << dp[j.ss][0] << endl;
        }
    
        // cout << sm << endl;
        ans.push_back(sm);
    }
 
    return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...