#include<cstdio>
#include<cmath>
#include<string>
#include<sstream>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
void init()
{
freopen("practicein.txt","r",stdin);
freopen("practiceout.txt","w",stdout);
}
int cmp(const int i, const int j)
{
return w[i] < w[j];
}
int find(int x)
{
return p[x] == x ? x : p[x] = find(p[x]);
}
int Kruskal()
{
int ans = 0;
for(int i = 1; i <= n; i++) p[i] = i;
for(int i = 1; i <= m; i++) r[i] = i;
sort(r+1, r+m+1, cmp);
for(int i = 1; i <= m; i++)
{
int e = r[i];
int x = find(u[e]), y = find(v[e]);
if(x != y)
{
p[x] = y;
ans += w[e];
}
}
return ans;
}
int main()
{
init();
readdata();
work();
return 0;
}
Kruskal
最新推荐文章于 2022-10-25 12:38:31 发布