이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
vector<int> par;
int find(int x){
if(par[x]==x) return x;
par[x] = find(par[x]);
return par[x];
}
void merge(int x, int y){
x = find(x);
y = find(y);
par[max(x,y)] = min(x,y);
}
int main(){
int n;
cin >> n;
vector<int> ps(n);
int m = 0;
for(int i=0;i<n;i++){
cin >> ps[i];
m = max(m,ps[i])+1;
}
// minimum spanning tree
vector<pair<int,pair<int,int>>> edges;
par = vector<int>(m);
for(int i=0;i<m;i++) par[i] = i;
for(int i=0;i<n;i++){
int l = ps[i];
for(int j=i;j<n;j++){
int r = ps[j];
edges.push_back({min(l%r,r%l),{l,r}});
}
}
sort(edges.begin(),edges.end());
int ans = 0;
int v = 0;
for(auto e:edges){
int a = e.second.first;
int b = e.second.second;
if(find(a)!=find(b)){
merge(a,b);
ans += e.first;
v++;
if(v==n-1) break;
}
}
cout << ans << endl;
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... |
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |