제출 #469551

#제출 시각아이디문제언어결과실행 시간메모리
469551ahmeterenSjekira (COCI20_sjekira)C++14
110 / 110
75 ms9928 KiB
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int size[N], mx[N], root[N]; vector<int> vec[N]; int find_set(int node) { if(root[node] == node) return node; return root[node] = find_set(root[node]); } void union_set(int a, int b) { a = find_set(a), b = find_set(b); if(size[a] < size[b]) swap(a, b); size[a] += size[b]; root[b] = a; mx[a] = max(mx[a], mx[b]); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, cevap = 0; cin >> n; vector<pair<int, int> > v(n + 1); for(int i = 1; i <= n; i++) { cin >> mx[i]; root[i] = i; size[i] = 1; v[i] = {mx[i], i}; } for(int i = 1; i < n; i++) { int u, v; cin >> u >> v; vec[u].push_back(v); vec[v].push_back(u); } sort(v.begin() + 1, v.end()); for(int i = 1; i <= n; i++) { int node = v[i].second; for(auto to : vec[node]) { if(mx[find_set(to)] <= v[i].first) { if(find_set(to) != find_set(node)) { cevap += v[i].first + mx[find_set(to)]; union_set(node, to); } } } } cout << cevap << '\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...