# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1242531 | hainam2k9 | Sjekira (COCI20_sjekira) | C++20 | 24 ms | 3260 KiB |
#include <bits/stdc++.h>
#define tt cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0)
#define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define db long double
#define sz(a) ((int)(a).size())
#define pb emplace_back
#define pf emplace_front
#define pob pop_back
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define ins emplace
#define mp make_pair
using namespace std;
const int MOD = 1e9+7, MAXN = 1e5+5;
const string NAME = "";
struct DSU{
int p[MAXN],sz[MAXN],MAX[MAXN];
void init(int n, int a[]){
for(int i = 1; i<=n; ++i)
p[i]=i, sz[i]=1, MAX[i]=a[i];
}
int Find(int u){
if(u==p[u]) return u;
return p[u]=Find(p[u]);
}
void Union(int x, int y){
if(sz[x]<sz[y]) swap(x,y);
p[y]=x, sz[x]+=sz[y], MAX[x]=max(MAX[x],MAX[y]);
}
}dsu;
int n,a[MAXN];
ll rs=0;
vector<pair<int,pair<int,int>>> v;
int main()
{
tt;
if(fopen((NAME + ".INP").c_str(), "r")) fo;
cin >> n;
for(int i = 1; i<=n; ++i)
cin >> a[i];
for(int i = 1; i<n; ++i){
int x,y;
cin >> x >> y;
v.pb(max(a[x],a[y]),mp(x,y));
}
sort(v.begin(),v.end());
dsu.init(n,a);
for(int i = 0; i<sz(v); ++i){
pair<int,int> p=v[i].se;
p.fi=dsu.Find(p.fi), p.se=dsu.Find(p.se);
if(p.fi!=p.se) rs+=dsu.MAX[p.fi]+dsu.MAX[p.se], dsu.Union(p.fi,p.se);
}
cout << rs;
}
컴파일 시 표준 에러 (stderr) 메시지
# | 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... |