이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define crep(i,x,n) for(int i=x;i<n;i++)
#define drep(i,n) for(int i=n-1;i>=0;i--)
#define vec(...) vector<__VA_ARGS__>
#define _3HaBFkZ ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
using pii=pair<int,int>;
using vi=vector<int>;
struct dsu{
int n,size_;
vector<int> par,siz,maxima;
dsu(int m,vi a){
init(m,a);
}
void init(int m,vi a){
n=m;
size_=m;
par.resize(n,0);
siz.resize(n,0);
maxima.resize(n,0);
for(int i=0;i<n;i++){
par[i]=i;
siz[i]=1;
maxima[i]=a[i];
}
}
void merge(int x,int y,int type=0){
int a=parent(x),b=parent(y);
if(a==b) return;
size_--;
if(siz[a]>siz[b]) {
siz[a]+=siz[b];
par[b]=a;
maxima[a]=max(maxima[a],maxima[b]);
}else{
siz[b]+=siz[a];
par[a]=b;
maxima[b]=max(maxima[b],maxima[a]);
}
}
int parent(int x){
return par[x]==x?x:parent(par[x]);
}
bool same(int x, int y){
return parent(x)==parent(y);
}
int size(){
return size_;
}
};
int main(){
_3HaBFkZ;
int n;
cin>>n;
vi a(n);
rep(i,n){
cin>>a[i];
}
vec(set<int>) adj(n);
rep(i,n-1){
int u,v;
cin>>u>>v;
u--,v--;
adj[u].insert(v);
adj[v].insert(u);
}
vec(pii) rbts;
rep(i,n){
rbts.pb({a[i],i});
}
sort(all(rbts));
reverse(all(rbts));
vec(pii) prcs;
rep(i,sz(rbts)){
int v=rbts[i].se;
ll val=rbts[i].fi;
for(auto u : adj[v]){
prcs.pb({v,u});
adj[u].erase(v);
}
}
reverse(all(prcs));
dsu uf(n,a);
ll ans=0;
rep(i,sz(prcs)){
int v=prcs[i].fi,u=prcs[i].se;
int par1=uf.parent(v),par2=uf.parent(u);
ans+=uf.maxima[par1];
ans+=uf.maxima[par2];
uf.merge(u,v);
}
cout<<ans<<"\n";
//
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sjekira.cpp: In function 'int main()':
sjekira.cpp:86:6: warning: unused variable 'val' [-Wunused-variable]
86 | ll val=rbts[i].fi;
| ^~~
# | 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... |