# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
725053 | vjudge1 | Kronican (COCI16_kronican) | C++17 | 1 ms | 340 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int MN=21;
int n,k;
int board[MN][MN];
int par[MN];
struct mst{
int left;
int right;
int weight;
}temp;
bool operator < (mst x,mst y){
return x.weight>y.weight;
}
void build(){
for(int i=1;i<=n;++i){
par[i]=-1;
}
}
int find(int u){
if(par[u]<0) return u;
return par[u]=find(par[u]);
}
void unionn(int a,int b){
a=find(a);
b=find(b);
if(a==b) return;
par[a]+=par[b];
par[b]=a;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
priority_queue<mst> pq;
cin>>n>>k;
build();
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
cin>>board[i][j];
if(i!=j){
temp.left=i;
temp.right=j;
temp.weight=board[i][j];
pq.push(temp);
}
}
}
int cnt=n-k,ans=0;
while(cnt>0){
temp=pq.top();
pq.pop();
int l=temp.left,r=temp.right;
if(find(l)==find(r)) continue;
unionn(l,r);
ans+=temp.weight;
--cnt;
}
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |