이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
//#include "temp.cpp"
#include <cstdio>
using namespace std;
#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif
#define sz(x) (int((x).size()))
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(x) (x).clear()
#define uniq(x) x.resize(unique(all(x)) - x.begin());
#define pb push_back
#define popf pop_front
#define popb pop_back
#define ld long double
#define ll long long
void print(long long t) {cerr << t;}
void print(int t) {cerr << t;}
void print(string t) {cerr << t;}
void print(char t) {cerr << t;}
void print(double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}
void print(long double t) {cerr << t;}
template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T> void print(T v[],T n) {cerr << "["; for(int i = 0; i < n; i++) {cerr << v[i] << " ";} cerr << "]";}
template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";}
template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(deque <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'
// for random generations
mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
// mt19937 myrand(131);
// for grid problems
int dx[8] = {-1,0,1,0,1,-1,1,-1};
int dy[8] = {0,1,0,-1,1,1,-1,-1};
// lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6
void fastIO() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
}
// file in/out
void setIO(string str = "") {
fastIO();
if (str != "") {
freopen((str + ".in").c_str(), "r", stdin);
freopen((str + ".out").c_str(), "w", stdout);
} else {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
}
// Indexed Set
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 2e5 + 10;
vector<int> d[N];
vector<pair<int, long long>> adj[N], adj2[N];
int n, deg[N], can[N];
long long sub, dp[N][2];
bool active[N], vis[N];
vector<int> curr;
int cnt = 0;
struct segTree {
struct node {
long long sum, cnt;
};
vector<node> mTree;
int size;
void init(long long n) {
size = 1;
while(size < n) {
size *= 2;
}
mTree.assign(2 * size - 1, {0, 0});
}
node combine(node a, node b) {
node c;
c.sum = a.sum + b.sum;
c.cnt = a.cnt + b.cnt;
return c;
}
void upd(int u, long long v, int x, int lx, int rx) { // set value at pos u
if(rx - lx == 1) {
if(v == 0) {
mTree[x] = {0, 0};
} else {
mTree[x] = {v, 1};
}
return;
}
int m = (lx + rx) / 2;
if(u < m) {
upd(u, v, 2 * x + 1, lx, m);
}else {
upd(u, v, 2 * x + 2, m, rx);
}
mTree[x] = combine(mTree[2 * x + 1], mTree[2 * x + 2]);
}
void upd(int u, long long v) {
upd(u, v, 0, 0, size);
}
long long get(int need, int x, int lx, int rx) {
if(rx - lx == 1) {
return mTree[x].sum;
}
int mid = (lx + rx) / 2;
if(mTree[2 * x + 1].cnt == need) {
return mTree[2 * x + 1].sum;
}
if(mTree[2 * x + 1].cnt > need) {
return get(need, 2 * x + 1, lx, mid);
} else {
return mTree[2 * x + 1].sum + get(need - mTree[2 * x + 1].cnt, 2 * x + 2, mid, rx);
}
}
long long get(int need) {
if(need == 0) {
return 0;
}
if(mTree[0].cnt < need) {
return -1;
}
if(mTree[0].cnt == need) {
return mTree[0].sum;
}
return get(need, 0, 0, size);
}
} seg[N];
void dfs(int node, int parent, int max_deg) {
int cnt = 0;
vis[node] = true;
for(auto i: adj2[node]) {
if(i.first == parent) continue;
dfs(i.first, node, max_deg);
}
vector<pair<long long, long long>> diff;
for(auto i: adj2[node]) {
if(i.first == parent) continue;
diff.push_back({dp[i.first][1] + i.second - dp[i.first][0], i.first});
}
sort(all(diff));
int curr_deg = deg[node];
{
int qn = curr_deg - max_deg;
long long to_add = 0, cr = 0;
vector<pair<long long, long long>> nd;
for(int i = 0; i < sz(diff); i++) {
if(diff[i].first < 0) {
to_add += diff[i].first + dp[diff[i].second][0];
cr++;
} else {
nd.push_back({diff[i].first + dp[diff[i].second][0], dp[diff[i].second][0]});
to_add += dp[diff[i].second][0];
}
}
if(cr >= qn) {
dp[node][0] = to_add;
} else {
long long an = qn - cr, qi = seg[node].get(an);
assert(sz(diff) + seg[node].mTree[0].cnt >= max_deg);
if(qi != -1) {
dp[node][0] = to_add + qi;
} else {
dp[node][0] = INT64_MAX / 10;
}
for(auto i: nd) {
to_add += i.first;
to_add -= i.second;
an--;
qi = seg[node].get(an);
if(qi != -1) {
dp[node][0] = min(dp[node][0], to_add + qi);
}
}
}
}
{
int qn = curr_deg - max_deg - 1;
long long to_add = 0, cr = 0;
vector<pair<long long, long long>> nd;
for(int i = 0; i < sz(diff); i++) {
if(diff[i].first < 0) {
to_add += diff[i].first + dp[diff[i].second][0];
cr++;
} else {
nd.push_back({diff[i].first + dp[diff[i].second][0], dp[diff[i].second][0]});
to_add += dp[diff[i].second][0];
}
}
if(cr >= qn) {
dp[node][1] = to_add;
} else {
long long an = qn - cr, qi = seg[node].get(an);
if(qi != -1) {
dp[node][1] = to_add + qi;
} else {
dp[node][1] = INT64_MAX / 10;
}
for(auto i: nd) {
to_add += i.first;
to_add -= i.second;
an--;
qi = seg[node].get(an);
if(qi != -1) {
dp[node][1] = min(dp[node][1], to_add + qi);
}
}
}
dp[node][0] = max(dp[node][0], dp[node][1]);
assert(dp[node][0] >= dp[node][1]);
}
}
bool cmp(pair<long long, long long> x, pair<long long, long long> y) {
return x.second < y.second;
}
map<pair<int, int>, int> mp[N];
map<pair<int, int>, int> mpil;
vector<long long> minimum_closure_costs(int N, vector<int> U, vector<int> V, vector<int> W) {
n = N;
for(int i = 0; i < n - 1; i++) {
// U[i]--, V[i]--;
adj[U[i]].emplace_back(V[i], W[i]);
adj[V[i]].emplace_back(U[i], W[i]);
deg[U[i]]++, deg[V[i]]++;
}
for(int i = 0; i < n; i++) {
d[deg[i]].push_back(i);
sort(all(adj[i]), cmp);
seg[i].init(deg[i] + 1);
int it = 0;
for(auto j: adj[i]) {
seg[i].upd(it, j.second);
mp[i][j] = it;
it++;
}
}
vector<long long> ans(n, 0);
for(int i = n - 2; i >= 0; i--) {
for(auto j: d[i + 1]) {
active[j] = true;
curr.push_back(j);
}
for(auto j: d[i + 1]) {
for(auto k: adj[j]) {
if(active[k.first]) {
if(!mpil[{j, k.first}]) {
adj2[k.first].emplace_back(j, k.second);
assert(mp[k.first].find({j, k.second}) != mp[k.first].end());
auto it = mp[k.first][{j, k.second}];
seg[k.first].upd(it, 0);
adj2[j].emplace_back(k.first, k.second);
assert(mp[j].find({k.first, k.second}) != mp[j].end());
it = mp[j][{k.first, k.second}];
seg[j].upd(it, 0);
mpil[{j, k.first}] = 1;
mpil[{k.first, j}] = 1;
}
}
}
}
for(auto j: curr) {
dp[j][0] = dp[j][1] = INT64_MAX / 10;
vis[j] = false;
}
for(auto j: curr) {
if(!vis[j]) {
// assert(j == 0);
dfs(j, -1, i);
ans[i] += dp[j][0];
}
}
}
return ans;
}
컴파일 시 표준 에러 (stderr) 메시지
roads.cpp: In function 'void dfs(int, int, int)':
roads.cpp:168:7: warning: unused variable 'cnt' [-Wunused-variable]
168 | int cnt = 0;
| ^~~
roads.cpp: In function 'void setIO(std::string)':
roads.cpp:69:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | freopen((str + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
roads.cpp:70:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | freopen((str + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
roads.cpp:72:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
roads.cpp:73:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |