[Noi39] subgraph

Small w favorite picture is a forest can be found (is not it amazing, in fact, easy to prove: If so ring ring itself is not illegal, if not ring then obviously legitimate). Continue found Erase minimum <=> choose sides maximum <=> maximum spanning forest, Kruskal run click.

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct ji{
 4     int x,y,z;
 5 }a[500001];
 6 int n,m,f[500001];
 7 long long ans;
 8 bool cmp(ji x,ji y){
 9     return x.z>y.z;
10 }
11 int find(int k){
12     if (k==f[k])return k;
13     return f[k]=find(f[k]);
14 }
15 int main(){
16     scanf("%d%d",&n,&m);
17     for(int i=1;i<=m;i++){
18         scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].z);
19         ans+=a[i].z;
20     }
21     sort(a+1,a+m+1,cmp);
22     for(int i=1;i<=n;i++)f[i]=i;
23     for(int i=1;i<=m;i++)
24         if (find(a[i].x)!=find(a[i].y)){
25             f[find(a[i].x)]=find(a[i].y);
26             ans-=a[i].z;
27         }
28     printf("%lld",ans);
29 }
View Code

 

Guess you like

Origin www.cnblogs.com/PYWBKTDA/p/11272282.html