# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
736158 | marvinthang | Sjekira (COCI20_sjekira) | C++17 | 37 ms | 4312 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/******************************
* author : @marvinthang *
* date : 27 / 12 / 2021 *
******************************/
#include <bits/stdc++.h>
using namespace std;
#define superspeed ios_base::sync_with_stdio(false); cin.tie(nullptr); //cout.tie(nullptr);
#define file(name) if (fopen (name".inp", "r") ) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
template <class U, class V> ostream & operator << (ostream& out, const pair<U, V> &p) { return out << '(' << p.first << ", " << p.second << ')'; }
template <class T> ostream & operator << (ostream &out, const vector<T> &vt) { out << '['; for (size_t i = 0; i + 1 < vt.size(); i++) out << vt[i] << ", "; if (!vt.empty()) out << vt.back(); return out << ']'; }
const int MOD = 1e9 + 7;
const double PI = 3.1415926535897932384626433832795; // acos(-1.0); atan(-1.0);
const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
const long long oo = 1e18;
const int MAX = 1e5 + 5;
int N, T[MAX];
struct Edge {
int u, v;
bool operator < (const Edge &other) const {
return max(T[u], T[v]) < max(T[other.u], T[other.v]);
}
} edges[MAX];
struct DisjointSet {
static const int MAXN = 1e5 + 5;
int par[MAXN] = {}, maxVal[MAXN];
DisjointSet(int n = 0) {
for (int i = 1; i <= n; ++i) par[i] = -1, maxVal[i] = T[i];
}
int findSet(int u) {
return par[u] < 0 ? u : par[u] = findSet(par[u]);
}
int unionSet(int u, int v) {
if ((u = findSet(u)) == (v = findSet(v))) return 0;
if (par[u] > par[v]) swap(u, v);
par[u] += par[v];
par[v] = u;
int res = maxVal[u] + maxVal[v];
maxVal[u] = max(maxVal[u], maxVal[v]);
return res;
}
};
int main(void) {
superspeed;
file("chvpt_sjekira");
cin >> N;
for (int i = 1; i <= N; ++i) cin >> T[i];
DisjointSet dsu(N);
for (int i = 1; i < N; ++i) cin >> edges[i].u >> edges[i].v;
sort(edges + 1, edges + N);
long long res = 0;
for (int i = 1; i < N; ++i) {
res += dsu.unionSet(edges[i].u, edges[i].v);
}
cout << res << '\n';
return 0;
}
Compilation message (stderr)
# | 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... |