# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
474080 | CaroLinda | Sjekira (COCI20_sjekira) | C++14 | 283 ms | 22448 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 all(x) x.begin(),x.end()
#define sz(x) (int)(x.size())
const int MAXN = 1e5+10 ;
using namespace std ;
int N ;
int dsu[MAXN] , t[MAXN] ;
set<pair<int,int>> mySet ;
set< pair<int,int> > s[MAXN] ;
int find(int x) { return dsu[x] = ( x == dsu[x] ) ? x : find(dsu[x]) ; }
void join(int x, int y)
{
x = find(x) ;
y = find(y) ;
if(x == y) return ;
if( sz(s[x]) > sz(s[y]) ) swap(x,y) ;
for(auto e : s[x])
if( find(e.second) != y && find(e.second) != x )
s[y].insert(e) ;
mySet.erase( make_pair(t[x] , x) ) ;
mySet.erase( make_pair(t[y] , y) ) ;
s[x].clear() ;
dsu[x] = y ;
t[y] = max(t[y] , t[x]) ;
mySet.insert( make_pair(t[y] , y) ) ;
}
int main()
{
scanf("%d", &N ) ;
for(int i = 1 ; i <= N ; i++ )
{
dsu[i] = i ;
scanf("%d", &t[i]) ;
mySet.insert(make_pair(t[i] , i)) ;
}
for(int i = 0 ,a , b ; i < N-1 ; i++ )
{
scanf("%d %d", &a, &b ) ;
s[a].insert(make_pair(t[b] , b)) ;
s[b].insert(make_pair(t[a] , a)) ;
}
long long tot = 0 ;
while(sz(mySet) > 1 )
{
pair<int,int> p = *mySet.begin() ;
auto it = s[p.second].begin() ;
while( it != s[p.second].end() && (it->second != find(it->second) || find(it->second) == p.second ) )
it = s[p.second].erase(it) ;
if(it == s[p.second].end()) return 0 ;
tot += p.first + t[it->second] ;
join(p.second, it->second ) ;
}
printf("%lld\n" , tot ) ;
}
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... |