# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
334606 | achibasadzishvili | Sjekira (COCI20_sjekira) | C++14 | 84 ms | 14956 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.
#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pb push_back
using namespace std;
ll n,p[200005],mx[200005],ans;
pair<ll,ll>a[200005];
vector<ll>v[200005];
ll find(ll x){
if(x == p[x])return x;
return p[x] = find(p[x]);
}
void join(ll x,ll y){
x = find(x);
y = find(y);
if(x == y)return;
ans += mx[x] + mx[y];
mx[x] = max(mx[x] , mx[y]);
p[y] = x;
}
int main(){
ios::sync_with_stdio(false);
cin >> n;
for(int i=1; i<=n; i++){
p[i] = i;
cin >> a[i].f;
mx[i] = a[i].f;
a[i].s = i;
}
for(int i=1; i<n; i++){
ll x,y;
cin >> x >> y;
if(a[x].f >= a[y].f)
v[x].pb(y);
if(a[y].f >= a[x].f)
v[y].pb(x);
}
sort(a + 1 , a + n + 1);
for(int o=1; o<=n; o++){
ll x = a[o].s;
for(int i=0; i<v[x].size(); i++){
ll y = v[x][i];
join(x , y);
}
}
cout << ans << '\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... |