洛谷P2820 局域网

 #include<bits/stdc++.h>
using namespace std;
const int maxn=;
const int maxm=;
int n,k,sum,tot;
struct node{
int cnt,fa;
}f[maxn];
int find(int x){return f[x].fa==x?x:f[x].fa=find(f[x].fa);}
void Union(int x,int y)
{
x=find(x),y=find(y);if(x==y) return;
if(f[x].cnt<=f[y].cnt) {f[x].fa=y;f[y].cnt+=f[x].cnt;}
else {f[y].fa=x;f[x].cnt+=f[y].cnt;}
}
struct edge{
int x,y,dis;
}E[maxm];
inline bool cmp(const edge &a,const edge &b) {return a.dis<b.dis;}
int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=k;i++) scanf("%d%d%d",&E[i].x,&E[i].y,&E[i].dis),sum+=E[i].dis;
for(int i=;i<=n;i++) f[i].fa=i,f[i].cnt=;
sort(E+,E++k,cmp);
for(int i=;i<=k;i++)
{
if(f[find()].cnt==n) break;
if(find(E[i].x)!=find(E[i].y)) Union(E[i].x,E[i].y),tot+=E[i].dis;
}
printf("%d",sum-tot);
return ;
}
上一篇:洛谷 P2820 局域网x


下一篇:Pooled genome sequence strategies |representative genome assembly approaches|Domestication|GERP|selective sweep|Hybridization|Introgression|iHS|SNP genotyping arrays|haplotype