Submission #374303

#TimeUsernameProblemLanguageResultExecution timeMemory
374303kartelSjekira (COCI20_sjekira)C++14
110 / 110
94 ms8928 KiB
#include <bits/stdc++.h> #define in(x) freopen(x, "r", stdin) #define out(x) freopen(x, "w", stdout) //#include <time.h> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> //#pragma GCC optimize("Ofast") //#pragma GCC optimize("unroll-loops") //#pragma GCC optimize("-O3") #define F first #define S second #define pb push_back //#define M ll(1e9 + 7) #define M ll(998244353) #define sz(x) (int)x.size() #define re return #define oo ll(1e18) #define el '\n' #define pii pair <int, int> #define all(x) (x).begin(), (x).end() #define arr_all(x, n) (x + 1), (x + 1 + n) #define vi vector<int> #define eps (ld)1e-9 using namespace std; typedef long long ll; //using namespace __gnu_pbds; //typedef tree <ll, null_type, less_equal <ll> , rb_tree_tag, tree_order_statistics_node_update> ordered_set; typedef double ld; typedef unsigned long long ull; typedef short int si; const int N = 1e5 + 500; int pr[N], mx[N], siz[N]; int n, cost[N], num[N]; vector <int> g[N]; ll ans; bool mk[N]; int f(int x) {return (x == pr[x] ? x : pr[x] = f(pr[x]));} void link(int x, int y) { x = f(x); y = f(y); if (siz[x] > siz[y]) { swap(x, y); } mx[y] = max(mx[x], mx[y]); pr[x] = y; siz[y] += siz[x]; } bool cmp(int _i, int _j) {return (cost[_i] > cost[_j]);} int main() { // mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());; ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); // in("toys.in"); // out("toys.out"); // in("input.txt"); // out("output.txt"); // cerr.precision(9); cerr << fixed; // clock_t tStart = clock(); cin >> n; for (int i = 1; i <= n; i++) { cin >> cost[i]; num[i] = i; } for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; g[u].pb(v); g[v].pb(u); } sort(arr_all(num, n), cmp); vector <pii> vc; for (int i = 1; i <= n; i++) { int v = num[i]; for (auto u : g[v]) { if (mk[u]) continue; vc.pb({u, v}); } mk[v] = 1; } reverse(all(vc)); for (int i = 1; i <= n; i++) { pr[i] = i; siz[i] = 1; mx[i] = cost[i]; } for (auto pi : vc) { int v = pi.F; int u = pi.S; if (f(v) == f(u)) { continue; } if (cost[v] < cost[u]) { swap(u, v); } ans += cost[v]; ans += mx[f(u)]; link(v, u); } cout << ans; } /* 7 4 6 7 2 3 1 5 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...