이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
long long n, a[100001], p[100001], sz[100001], sum = 0;
pair<int, pair<int, int>> edge[100001];
int findparent(int x)
{
if(x == p[x]) return x;
else return p[x] = findparent(p[x]);
}
int main()
{
cin >> n;
for(int i = 1; i <= n; ++i)
{
cin >> a[i];
p[i] = i;
sz[i] = a[i];
}
for(int i = 1; i < n; ++i)
{
cin >> edge[i].second.first >> edge[i].second.second;
edge[i].first = max(a[edge[i].second.first], a[edge[i].second.second]);
}
sort(edge + 1, edge + n);
for(int i = 1; i < n; ++i)
{
long long a = findparent(edge[i].second.first), b = findparent(edge[i].second.second);
sum = sum + sz[a] + sz[b];
if(a != b)
{
p[a] = b;
sz[b] = max(sz[a], sz[b]);
}
}
cout << sum;
}
# | 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... |