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<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;
template<class X, class Y>
inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);}
template<class X, class Y>
inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
const int MAXN = 100005;
class DisjointSet {
private:
vector<int> lab;
public:
DisjointSet(int _n = 0) {
lab.assign(_n + 7, -1);
}
int find(int u) {
return (lab[u] < 0) ? u : (lab[u] = find(lab[u]));
}
bool join(int u, int v) {
u = find(u), v = find(v);
if(u == v)
return (false);
if(lab[u] > lab[v])
swap(u, v);
lab[u] += lab[v];
lab[v] = u;
return (true);
}
} dsu;
struct Edge {
int u, v, w;
} edge[MAXN];
int deg[MAXN], numNode;
vector<ll> minimum_closure_costs(int _N, vector<int> _U, vector<int> _V, vector<int> _W) {
numNode = _N;
ll sumCost(0);
for (int i = 0; i + 1 < numNode; ++i) {
edge[i] = {_U[i], _V[i], _W[i]};
sumCost += edge[i].w;
}
sort(edge, edge + numNode - 1, [](const Edge &a, const Edge &b) {
return (a.w > b.w);
});
vector<ll> ans;
for (int k = 0; k < numNode; ++k) {
for (int u = 0; u < numNode; ++u)
deg[u] = 0;
dsu = DisjointSet(numNode);
ll res(0);
for (int i = 0; i < numNode - 1; ++i) {
int u(edge[i].u), v(edge[i].v), w(edge[i].w);
if(max(deg[u], deg[v]) < k) {
++deg[u], ++deg[v];
dsu.join(u, v);
res += w;
}
}
ans.push_back(sumCost - res);
}
return ans;
}
#ifdef Nhoksocqt1
int main(void) {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define TASK "roads"
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
vector<int> u, v, w;
int n;
cin >> n;
u.resize(n - 1), v.resize(n - 1), w.resize(n - 1);
for (int i = 0; i + 1 < n; ++i) {
cin >> u[i] >> v[i] >> w[i];
}
vector<ll> ans = minimum_closure_costs(n, u, v, w);
cout << "ANSWER: ";
for (int it = 0; it < sz(ans); ++it)
cout << ans[it] << ' '; cout << '\n';
return 0;
}
#endif // Nhoksocqt1
# | 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... |