Submission #391270

#TimeUsernameProblemLanguageResultExecution timeMemory
391270MrRobot_28Sjekira (COCI20_sjekira)C++17
0 / 110
79 ms15856 KiB
#include<bits/stdc++.h> using namespace std; #define X first #define Y second #define sz(a) (int)a.size() #define ll long long #define ld long double const int N = 3e5 + 100; int dsu[N]; int val[N]; int rang[N]; vector <pair <int, int> > vec[N]; int pred(int a) { if(a == dsu[a]) return a; return dsu[a] = pred(dsu[a]); } void unite(int a, int b) { a = pred(a); b = pred(b); if(rang[b] < rang[a]) { swap(a, b); } dsu[b] = a; rang[a] += rang[b]; val[a] = max(val[a], val[b]); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector <pair <int, int> > a(n); for(int i = 0; i < n; i++) { cin >> a[i].X; val[i] = a[i].X; a[i].Y = i; dsu[i] = i; rang[i] = 1; } sort(a.begin(), a.end()); vector <vector <int> > g(n); for(int i = 0; i < n - 1; i++) { int a, b; cin >> a>> b; a--; b--; vec[a].push_back({a, b}); vec[b].push_back({b, a}); g[a].push_back(b); g[b].push_back(a); } ll s = 0; for(int i = 0; i < n; i++) { int minto = -1; int v = a[i].Y; for(int j = 0; j < sz(g[v]); j++) { if(pred(g[v][j]) != pred(v)) { if(minto == -1 || val[pred(g[v][j])] < val[pred(minto)]) { minto = g[v][j]; } } } if(minto != -1) { s += val[pred(v)] + val[pred(minto)]; unite(minto, v); } } cout << s; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...