Submission #985154

# Submission time Handle Problem Language Result Execution time Memory
985154 2024-05-17T11:41:49 Z Ghetto Road Closures (APIO21_roads) C++17
0 / 100
2000 ms 21488 KB
#include "roads.h"
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pii = pair<int, int>;
using pll = pair<lint, lint>;
const int MAX_N = 1e5 + 5;

int n;
vector<int> adj[MAX_N];

int n_inds, ind[MAX_N];
vector<int> children[MAX_N];
bool size_cmp(int u, int v) { return adj[u].size() > adj[v].size(); }
void dfs1(int u, int par = -1) {
    n_inds++, ind[u] = n_inds;
    for (int v : adj[u])
        if (v != par) dfs1(v, u), children[u].push_back(v);
    sort(children[u].begin(), children[u].end(), size_cmp);
}
vector<int> size_ord;
void precomp() {
    dfs1(0, -1);
    for (int u = 0; u < n; u++) size_ord.push_back(u);
    sort(size_ord.begin(), size_ord.end(), size_cmp);
}

unordered_set<int> seen;
lint dp[MAX_N][2];
void dfs2(int u, int k) {
    seen.insert(u);
    int c0 = adj[u].size() - k, c1 = adj[u].size() - k - 1;
    multiset<pll> vals;
    for (int v : children[u]) {
        if (adj[v].size() <= k) break;
        dfs2(v, k);
        vals.insert({1 + dp[v][true] - dp[v][false], 1 + dp[v][true]});
    }

    dp[u][false] = dp[u][true] = 0;
    while (vals.size() && (c0 || c1)) {
        if (c0) c0--, dp[u][false] += vals.rbegin()->second;
        if (c1) c1--, dp[u][true] += vals.rbegin()->second;
        vals.erase(next(vals.end(), -1));
    }
}
lint solve_k(int k) {
    vector<pii> bigs;
    lint ans = 0;
    for (int u : size_ord) {
        if (adj[u].size() <= k) break;
        bigs.push_back({ind[u], u});
        ans += adj[u].size() - k;
    }
    sort(bigs.begin(), bigs.end());

    // cout << k << ": ";
    // for (pii x : bigs) cout << x.second << " ";
    // cout << endl;

    seen = {};
    for (pii x : bigs) {
        int u = x.second;
        if (seen.count(u)) continue;
        dfs2(u, k);
        ans -= dp[u][false];
    }

    // if (k == 0) {
    //     for (int u = 0; u < n; u++) cout << u << ": " << dp[u][0] << " " << dp[u][1] << endl;
    // }

    return ans;
}

vector<lint> minimum_closure_costs(int tmp_n, vector<int> tmp_e1, vector<int> tmp_e2, vector<int> tmp_cost) {
    n = tmp_n;
    for (int i = 0; i < n - 1; i++) 
        adj[tmp_e1[i]].push_back(tmp_e2[i]), adj[tmp_e2[i]].push_back(tmp_e1[i]);
    
    precomp();

    vector<lint> ans;
    for (int k = 0; k < n; k++) ans.push_back(solve_k(k));
    return ans;



    // for (int u = 0; u < n; u++) {
    //     cout << u << ": ";
    //     for (int v : children[u]) cout << v << " ";
    //     cout << endl;
    // }
}

Compilation message

roads.cpp: In function 'void dfs2(int, int)':
roads.cpp:35:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   35 |         if (adj[v].size() <= k) break;
      |             ~~~~~~~~~~~~~~^~~~
roads.cpp: In function 'lint solve_k(int)':
roads.cpp:51:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   51 |         if (adj[u].size() <= k) break;
      |             ~~~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 6744 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 6748 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 6748 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 6748 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2049 ms 21488 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2049 ms 21488 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 6744 KB Output isn't correct
2 Halted 0 ms 0 KB -