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;
const int N = 1e5 + 5;
int size[N], mx[N], root[N];
vector<int> vec[N];
int find_set(int node)
{
if(root[node] == node)
return node;
return root[node] = find_set(root[node]);
}
void union_set(int a, int b)
{
a = find_set(a), b = find_set(b);
if(size[a] < size[b])
swap(a, b);
size[a] += size[b];
root[b] = a;
mx[a] = max(mx[a], mx[b]);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, cevap = 0;
cin >> n;
vector<pair<int, int> > v(n + 1);
for(int i = 1; i <= n; i++)
{
cin >> mx[i];
root[i] = i;
size[i] = 1;
v[i] = {mx[i], i};
}
for(int i = 1; i < n; i++)
{
int u, v;
cin >> u >> v;
vec[u].push_back(v);
vec[v].push_back(u);
}
sort(v.begin() + 1, v.end());
for(int i = 1; i <= n; i++)
{
int node = v[i].second;
for(auto to : vec[node])
{
if(mx[find_set(to)] <= v[i].first)
{
if(find_set(to) != find_set(node))
{
cevap += v[i].first + mx[find_set(to)];
union_set(node, to);
}
}
}
}
cout << cevap << '\n';
return 0;
}
# | 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... |