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 <vector>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll N1 = 1e5;
vector<int> chain(N1);
vector<int> portion(N1, 1);
int find(int x) {
while (x != chain[x]) x = chain[x];
return x;
}
bool same(int a, int b) {
return find(a) == find(b);
}
void unite(int a, int b) {
a = find(a);
b = find(b);
if (portion[a] < portion[b]) swap(a,b);
portion[a] += portion[b];
chain[b] = a;
}
bool cmp(tuple<ll, ll, ll> t1, tuple<ll, ll, ll> t2)
{
return (get<2>(t1) > get<2>(t2));
}
std::vector<long long> minimum_closure_costs(int N, std::vector<int> U,
std::vector<int> V,
std::vector<int> W) {
vector<tuple<ll, ll, ll>> edge(N-1);
for (int i=0; i<N-1; i++)
{
edge[i] = {U[i], V[i], W[i]};
}
sort(edge.begin(), edge.end(), cmp);
for (int i=0; i<N; i++) {chain[i] = i;}
ll reduction = 0;
ll total = 0;
for (auto e: edge)
{
ll u = get<0>(e); ll v = get<1>(e); ll c = get<2>(e);
total += c;
if (portion[find(u)] == 1 && portion[find(v)] == 1) {unite(u, v); reduction += c;}
}
vector<ll> answer = {total, total - reduction};
for (int i=0; i<N-2; i++) {answer.push_back(0);}
return answer;
}
# | 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... |