#include<iostream>
#include<algorithm>
using namespace std;
const int N=200010;
int n,m;
int p[N];
struct node{
    int a,b,w;
    bool operator<(const node &t){
        return w<t.w;
    }
};
node edge[N];

int find(int x){
    if(x!=p[x]) p[x]=find(p[x]);
    return p[x];
}

int main(int argc, char *argv[])
{
    cin>>n>>m;
    for(int i=0;i<m;i++){
        int a,b,w;
        cin>>a>>b>>w;
        edge[i]={a,b,w};
    }
    sort(edge,edge+m);
    int res,k;
    for(int i=1;i<=n;i++) p[i]=i;
    for(int i=0;i<m;i++){
        int a=edge[i].a;
        int b=edge[i].b;
        int w=edge[i].w;
        if(find(a)!=find(b)){
            k++;
            res+=w;
            p[find(a)]=find(b);
        }
    }
  
    if(k<n-1) cout<<"impossible"<<endl;

    else cout<<res<<endl;
    return 0;
}


 posted on 2019-08-22 15:15  谁是凶手1703  阅读(44)  评论(0)    收藏  举报