답안 #985161

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
985161 2024-05-17T11:47:59 Z Ghetto 도로 폐쇄 (APIO21_roads) C++17
0 / 100
2000 ms 20224 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);
}

int checker;
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]});
    }
    checker += vals.size(); assert(checker <= 1e7);

    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());

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

Compilation message

roads.cpp: In function 'void dfs2(int, int)':
roads.cpp:36:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   36 |         if (adj[v].size() <= k) break;
      |             ~~~~~~~~~~~~~~^~~~
roads.cpp: In function 'lint solve_k(int)':
roads.cpp:53:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   53 |         if (adj[u].size() <= k) break;
      |             ~~~~~~~~~~~~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 6748 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 6748 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 6748 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 6748 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2068 ms 20224 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2068 ms 20224 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 6748 KB Output isn't correct
2 Halted 0 ms 0 KB -