제출 #374302

#제출 시각아이디문제언어결과실행 시간메모리
374302NONAMESjekira (COCI20_sjekira)C++17
110 / 110
85 ms11236 KiB
#include <bits/stdc++.h> using namespace std; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); const int man = (int)(1e5 + 500); int n; int sz[man], c[man], pr[man], x[man], y[man]; long long a[man], val[man]; bool was[man]; vector <array <int, 2> > g[man]; vector <array <int, 2> > ops; int f(int x) { return (x == pr[x]) ? x : pr[x] = f(pr[x]); } void un(int x, int y) { x = f(x); y = f(y); if (sz[x] < sz[y]) { swap(x, y); } val[x] = max(val[x], val[y]); sz[x] += sz[y]; pr[y] = x; } bool cmp(int x, int y) { return (a[x] > a[y]); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; c[i] = i; pr[i] = i; val[i] = a[i]; sz[i] = 1; } for (int i = 0; i < (n - 1); ++i) { cin >> x[i] >> y[i]; --x[i], --y[i]; g[x[i]].push_back({y[i], i}); g[y[i]].push_back({x[i], i}); } sort(c, c + n, cmp); for (int i = 0; i < n; ++i) { int v = c[i]; for (auto& u : g[v]) { if (was[u[1]] == true) { continue; } was[u[1]] = true; ops.push_back({v, u[0]}); } } long long ans = 0; reverse(ops.begin(), ops.end()); // cerr << (int)(ops.size()) << "\n"; for (auto& i : ops) { ans += val[f(i[0])] + val[f(i[1])]; un(i[0], i[1]); } cout << ans << "\n"; 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...