class Main{ public static int N = 100010, n, m, M = 200010; public static int[] p = new int[N]; public static Edge[] edge;//不能是Edge[] edge = new Edge[M],后面没有创建对象。 public static int find(int x){ if(p[x] != x)p[x] = find(p[x]); return p[x]; } public static int kruskal(){ //Kruskal算法 Arrays.sort(edge);//将所有边按权重从小到大排序 //初始化并查集 for(int i = 1; i <= n; i++)p[i] = i; int res = 0, cnt = 0;//res记录最小生成树的树边权重之和, cnt记录当前加入的边数 //从小到大枚举所有边 for(int i = 0; i < m; i++){ int a = edge[i].a, b = edge[i].b, w = edge[i].w; a = find(a); b = find(b); if(a != b){//判断两个节点是否连通,两个节点不连通 p[a] = b; //将两个集合合并 res += w; cnt++; } } return cnt < n - 1 ? -1 : res; } public static void main(String[] args){ Scanner sc = new Scanner(new BufferedInputStream(System.in)); n = sc.nextInt(); m = sc.nextInt(); edge = new Edge[m]; for(int i = 0; i < m; i++){ int a = sc.nextInt(), b = sc.nextInt(), w = sc.nextInt(); edge[i] = new Edge(a, b, w); } int res = kruskal(); if(res == -1)System.out.println("impossible"); //说明图不连通 else System.out.print(res); } static class Edge implements Comparable<Edge>{ int a, b, w; public Edge(int a, int b, int w){ this.a = a; this.b = b; this.w = w; } public int compareTo(Edge o){ return this.w-o.w; } } }