이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "roads.h"
#include <bits/stdc++.h>
//PAIN ^ 2
using namespace std;
const int N = 1e5 + 5;
const long long INF = 1ll << 60;
long long dp[N][2];
int X, p[N], deg[N];
set<array<int, 2>> adj[N];
vector<array<int, 2>> adjV[N];
vector<int> rec;
set<int> S;
bitset<N> enb, mark;
struct segment_tree {
vector<long long> sum, cnt;
int n = 0, kol = 0;
void init(int _n) {
n = _n;
sum.assign(4 * n, 0);
cnt.assign(4 * n, 0);
}
long long get(int x) {
if (x <= 0) return (x == 0 ? 0 : -INF);
int l = 0, r = n, v = 1, s = 0;
while (r - l > 1) {
int m = l + r >> 1;
v <<= 1;
if (cnt[v] >= x) r = m;
else x -= cnt[v], s += sum[v], v |= 1, l = m;
}
return s + sum[v];
}
void add(int id, int x, int l, int r, int v = 1) {
if (l + 1 == r) {
kol += x;
sum[v] += x;
cnt[v]++;
return;
}
int m = l + r >> 1;
if (id < m) add(id, x, l, m, v << 1);
else add(id, x, m, r, v << 1 | 1);
sum[v] = sum[v << 1] + sum[v << 1 | 1];
cnt[v] = cnt[v << 1] + cnt[v << 1 | 1];
}
} SEG[N];
void dfs(int v, int p) {
if (enb[v]) assert(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());
dp[v][0] = dp[v][1] = 0;
for (int i = 0; i <= X && i <= V.size(); i++) {
if (i) ans += V[i - 1];
dp[v][0] = max(dp[v][0], SEG[v].get(X - i) + ans + dif);
dp[v][1] = max(dp[v][1], SEG[v].get(X - i - 1) + ans + dif);
//if (v == 0)
//cout << "i: " << i << " (chosen from V) that is " << ans << " SEG: " << SEG[v].get(X - i) << endl;
}
//cout << "V: " << v << " SEG: " << SEG[v].kol << " dp[0] " << dp[v][0] << " dp[1] " << dp[v][1] << endl;
}
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]].insert({V[i], W[i]});
adj[V[i]].insert({U[i], W[i]});
adjV[U[i]].push_back({-W[i], V[i]});
adjV[V[i]].push_back({-W[i], U[i]});
all += W[i];
}
for (int i = 0; i < n; i++) sort(adjV[i].begin(), adjV[i].end()), SEG[i].init(adjV[i].size());
iota(p, p + n, 0), sort(p, p + n, [&](const int x, const int y) {return adjV[x].size() < adjV[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++) { //TODO
X = i;
for (;ptr < n && adjV[p[ptr]].size() <= i; ptr++) {
int v = p[ptr];
array<int, 2> P;
for (auto P: adjV[v]) {
int u = P[1], w = -P[0];
if (enb[u]) all -= w;
else {
SEG[u].add(lower_bound(adjV[u].begin(), adjV[u].end(), array<int, 2>{-w, v}) - adjV[u].begin(), w, 0, adjV[u].size(), 1); //at loc P[1] and v
//cout << "ADDING TO " << u << " edge: " << w << endl;
}
adj[v].erase(P);
adj[u].erase({v, w});
}
enb[v] = true;
S.erase(v);
}
long long ans = 0;
for (auto v: S) if (!mark[v]) {
//cout << "DFS: " << v << endl;
dfs(v, v);
ans += dp[v][0];
}
for (auto v: rec) mark[v] = 0;
rec.clear();
re[i] = all - ans;
}
return re;
}
컴파일 시 표준 에러 (stderr) 메시지
roads.cpp: In member function 'long long int segment_tree::get(int)':
roads.cpp:27:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
27 | int m = l + r >> 1;
| ~~^~~
roads.cpp: In member function 'void segment_tree::add(int, int, int, int, int)':
roads.cpp:41:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
41 | int m = l + r >> 1;
| ~~^~~
roads.cpp: In function 'void dfs(int, int)':
roads.cpp:68:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for (int i = 0; i <= X && i <= V.size(); i++) {
| ~~^~~~~~~~~~~
roads.cpp:55:30: warning: unused variable 'mx' [-Wunused-variable]
55 | long long dif = 0, ans = 0, mx = 0;
| ^~
roads.cpp: In function 'std::vector<long long int> minimum_closure_costs(int, std::vector<int>, std::vector<int>, std::vector<int>)':
roads.cpp:80:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for (int i = 0; i < U.size(); i++) {
| ~~^~~~~~~~~~
roads.cpp:96:40: warning: comparison of integer expressions of different signedness: 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
96 | for (;ptr < n && adjV[p[ptr]].size() <= i; ptr++) {
| ~~~~~~~~~~~~~~~~~~~~^~~~
roads.cpp:98:18: warning: unused variable 'P' [-Wunused-variable]
98 | array<int, 2> P;
| ^
# | 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... |