이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ll mask){return __builtin_ctzll(mask);}
int logOf(ll mask){return __lg(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T& container, char separator = ' ', char finish = '\n'){
for(auto item: container) cout << item << separator;
cout << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
struct DSU{
int n;
vector<int> parent, sz;
vector<int> ma;
DSU(int _n){
n = _n;
parent.resize(n+1); sz.resize(n+1, 1);
ma.resize(n+1);
for(int i = 1; i<=n; ++i) parent[i] = i;
}
int find_set(int u){return (u == parent[u]) ? u : (parent[u] = find_set(parent[u]));}
bool same_set(int u, int v){return find_set(u) == find_set(v);}
bool join_set(int u, int v){
u = find_set(u), v = find_set(v);
if (u != v){
if (sz[u] < sz[v]) swap(u, v);
parent[v] = u;
sz[u] += sz[v];
maximize(ma[u], ma[v]);
return true;
}
return false;
}
int get_size(int u){return sz[find_set(u)];}
int get_ma(int u){return ma[find_set(u)];}
};
void solve(){
int n; cin >> n;
vector<int> a(n+1);
for(int i = 1; i<=n; ++i) cin >> a[i];
vector<pair<int, int>> edge(n-1);
vector<array<int, 3>> weighted_edge(n-1);
for(int i = 0; i<n-1; ++i){
int u, v; cin >> u >> v;
edge[i] = {u, v};
weighted_edge[i] = {{max(a[u], a[v]), u, v}};
}
sort(ALL(weighted_edge));
DSU mst(n);
for(int i = 1; i<=n; ++i) mst.ma[i] = a[i];
ll ans = 0;
for(auto i: weighted_edge){
int u = i[1], v = i[2];
ans += mst.get_ma(u) + mst.get_ma(v);
mst.join_set(u, v);
}
cout << ans << "\n";
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
clock_t start = clock();
solve();
cerr << "Time elapsed: " << clock() - start << "ms!\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |