思路:克魯斯卡爾模板
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define SIS std::ios::sync_with_stdio(false)
#define space putchar(' ')
#define enter putchar('\n')
typedef pair<int,int> PII;
const int mod=1e4+7;
const int N=2e6+10;
const int inf=0x7f7f7f7f;
ll gcd(ll a,ll b)
{
return b==0?a:gcd(b,a%b);
}
ll lcm(ll a,ll b)
{
return a*(b/gcd(a,b));
}
template <class T>
void read(T &x)
{
char c;
bool op = 0;
while(c = getchar(), c < '0' || c > '9')
if(c == '-')
op = 1;
x = c - '0';
while(c = getchar(), c >= '0' && c <= '9')
x = x * 10 + c - '0';
if(op)
x = -x;
}
template <class T>
void write(T x)
{
if(x < 0)
x = -x, putchar('-');
if(x >= 10)
write(x / 10);
putchar('0' + x % 10);
}
int n,m;
int p[N];
struct Edge
{
int a,b,w;
bool operator <(const Edge x)const
{
return w<x.w;
}
}edge[N];
int fnd(int x)
{
if(p[x]!=x)p[x]=fnd(p[x]);
else return p[x];
}
int main()
{
SIS;
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);
for(int i=0;i<=n;i++)
p[i]=i;
int res=0,cnt=0;
for(int i=0;i<m;i++)
{
int a=edge[i].a,b=edge[i].b,w=edge[i].w;
a=fnd(a),b=fnd(b);
if(a!=b)
{
p[a]=b;
res+=w;cnt++;
}
}
if(cnt<n-1)puts("impossible");
else cout<<res<<endl;
return 0;
}