Submission #336708

# Submission time Handle Problem Language Result Execution time Memory
336708 2020-12-16T13:01:00 Z neki Sjekira (COCI20_sjekira) C++14
0 / 110
113 ms 18284 KB
#include <bits/stdc++.h>
#define loop(i, a, b) for(long long i=a;i<b;i++)
#define pool(i, a, b) for(long long i=a-1;i>=b;i--)
#define fore(i, a) for(auto&& i:a)
#define fi first
#define se second
#define ps(a) push_back(a)
#define pb(a) pop_back(a)
#define sc scanf
#define vc vector
#define ll long long
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define llmax LLONG_MAX/2
#define llmin -LLONG_MAX/2
using namespace std;
#define mn 101000
#define par pair<ll, ll>
#define ld long double
#define mod 1000000007
ll arr[mn], dsu[mn], pos[mn], lar[mn], siz[mn];
par srt[mn];
vc<ll> edg[mn];
ll fnd(ll u){return (u==dsu[u]) ? u:fnd(dsu[u]);}
int main() {
    ll n;cin >> n;
    loop(i, 1, n+1) cin >> arr[i];
    loop(i, 1, n+1) srt[i].fi=arr[i], srt[i].se=i, dsu[i]=i, siz[i]=i;
    sort(srt+1, srt+n+1);
    loop(i, 1, n+1) lar[srt[i].se]=i;
    loop(i, 0, n-1){
        ll a, b;cin >> a >> b;
        edg[a].ps(b);
        edg[b].ps(a);
    }

    ll ans=0;
    loop(i, 1, n+1){
        ll u=srt[i].se;
        fore(v, edg[u]){ 
            ll pv=fnd(v),  pu=fnd(u);
            if(lar[pv]<lar[pu]){ 
                ans+=lar[srt[lar[pu]].fi]+lar[srt[lar[pv]].fi];
                if(siz[pv]>siz[pu]) swap(pv, pu);
                dsu[pv]=pu;
                lar[pu]=i;
                siz[pu]+=siz[pv];
            }
        }
    }
    cout << ans << endl;
}
# Verdict Execution time Memory Grader output
1 Runtime error 8 ms 5228 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 113 ms 18284 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 8 ms 5228 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 8 ms 5228 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -