제출 #391269

#제출 시각아이디문제언어결과실행 시간메모리
391269MrRobot_28Sjekira (COCI20_sjekira)C++17
0 / 110
62 ms15892 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[i]); j++) { if(pred(g[i][j]) != pred(v)) { if(minto == -1 || val[pred(g[i][j])] < val[pred(minto)]) { minto = g[i][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...