This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "roads.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
long long dp[N][2];
int X, p[N];
vector<array<int, 2>> adj[N];
vector<int> rec;
set<int> S;
bitset<N> enb, mark;
void dfs(int v, int p) {
if (enb[v]) return void(dp[v][0] = dp[v][1] = 0);
rec.push_back(v);
mark[v] = true;
long long dif = 0, ans = 0, mx = 0;
vector<long long> V;
V.reserve(adj[v].size());
for (auto P: adj[v]) {
int u = P[0], w = P[1];
if (u == p) continue;
dfs(u, v);
dif += dp[u][0];
V.push_back(w + dp[u][1] - dp[u][0]);
}
sort(V.rbegin(), V.rend());
for (int i = 0; i <= X && i <= V.size(); i++) {
if (i) ans += V[i - 1];
mx = max(mx, ans);
if (i + 1 == X) dp[v][1] = mx + dif;
}
if (V.size() + 1 < X) dp[v][1] = mx + dif;
dp[v][0] = mx + dif;
}
vector<long long> minimum_closure_costs(int n, vector<int> U, vector<int> V, vector<int> W) {
long long all = 0;
for (int i = 0; i < U.size(); i++) {
adj[U[i]].push_back({V[i], W[i]});
adj[V[i]].push_back({U[i], W[i]});
all += W[i];
}
iota(p, p + n, 0), sort(p, p + n, [&](const int x, const int y) {return adj[x].size() < adj[y].size();});
for (int i = 0; i < n; i++) S.insert(i);
vector<long long> re(n);
re[0] = all;
for (int i = 1, ptr = 0; i < n; i++) {
X = i;
for (;ptr < n && adj[p[ptr]].size() <= i; ptr++) {
int v = p[ptr];
for (auto P: adj[v]) if (enb[P[0]]) all -= P[1];
enb[v] = true;
S.erase(v);
}
long long ans = 0;
for (auto v: S) if (!mark[v]) {
dfs(v, v);
ans += dp[v][0];
}
if (i >= 500) assert(rec.size() <= 1000);
for (auto v: rec) mark[v] = 0;
rec.clear();
re[i] = all - ans;
}
return re;
}
Compilation message (stderr)
roads.cpp: In function 'void dfs(int, int)':
roads.cpp:30:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for (int i = 0; i <= X && i <= V.size(); i++) {
| ~~^~~~~~~~~~~
roads.cpp:35:19: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
35 | if (V.size() + 1 < X) dp[v][1] = mx + dif;
| ~~~~~~~~~~~~~^~~
roads.cpp: In function 'std::vector<long long int> minimum_closure_costs(int, std::vector<int>, std::vector<int>, std::vector<int>)':
roads.cpp:41:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for (int i = 0; i < U.size(); i++) {
| ~~^~~~~~~~~~
roads.cpp:53:39: warning: comparison of integer expressions of different signedness: 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
53 | for (;ptr < n && adj[p[ptr]].size() <= i; ptr++) {
| ~~~~~~~~~~~~~~~~~~~^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |