码迷,mamicode.com
首页 > 其他好文 > 详细

CF F. Royal Questions kruskal

时间:2019-07-29 14:52:07      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:scanf   put   c_str   mes   print   set   NPU   input   ons   

Code: 

#include<bits/stdc++.h> 
#define ll long long  
#define maxn 200004 
using namespace std;  
void setIO(string s) {
    string in=s+".in"; 
    freopen(in.c_str(),"r",stdin); 
}
int n,m;   
int p[maxn],tag[maxn]; 
void init() {
    for(int i=0;i<maxn;++i) p[i]=i;  
} 
int find(int x) {
    return p[x]==x?x:p[x]=find(p[x]);  
}
struct Edge { 
    int u,v,c; 
}ed[maxn];         
bool cmp(Edge a,Edge b) {
    return a.c>b.c; 
}
int main() {
    // setIO("input");  
    init(); 
    scanf("%d%d",&n,&m);   
    for(int i=1;i<=m;++i) scanf("%d%d%d",&ed[i].u,&ed[i].v,&ed[i].c);   
    sort(ed+1,ed+1+m,cmp);  
    ll ans=0; 
    for(int i=1;i<=m;++i) {
        int a=find(ed[i].u), b=find(ed[i].v);       
        if(a!=b&&(!tag[a]||!tag[b])) ans+=1ll*ed[i].c, tag[a]|=tag[b], p[b]=a; 
        else if(a==b && !tag[a]) ans+=1ll*ed[i].c, tag[a]=1;         
    }   
    printf("%I64d\n",ans); 
    return 0; 
}

  

CF F. Royal Questions kruskal

标签:scanf   put   c_str   mes   print   set   NPU   input   ons   

原文地址:https://www.cnblogs.com/guangheli/p/11263392.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!