# Include <iostream> # include <stdio. h> # include <string. h> # define max 20 using namespace STD; struct edge {int U, V, W;} edges [Max]; int parent [Max]; int n, m; int I, j; void ufset () {for (I = 1; I <= N; I ++) parent [I] =-1;} int find (INT X) {int s; For (S = x; parent [s]> = 0; S = parent [s]); While (s! = X) {int TMP = parent [X]; parent [x] = s; X = TMP;} return s;} void Union (INT R1, int R2) {int temp; int R1 = find (R1); int r2 = find (R2); int TMP = parent [R1] + parent [R2]; if (parent [R1]> parent [R2]) // r2> R1 {parent [R1] = R2; parent [R2] = TMP ;} else {parent [R2] = R1; parent [R1] = temp;} int CMP (const void * a, const void * B) {edge AA = * (const edge *) A; edge BB = * (const edge *) B; return AA. w-bb.w;} void Kruskal () {int sumweight = 0; int num = 0; int U, V; ufset (); for (I = 0; I <m; I ++) {u = edges [I]. u; V = edges [I]. v; If (find (u )! = Find (V) {printf ("% d \ n", U, V, edges [I]. w); sumweight + = edges [I]. w; num ++; Union (u, v);} If (Num> = N-1) break;} printf ("weight of MST is % d \ n ", sumweight) ;}int main () {int U, V, W; scanf ("% d", & N, & M); For (INT I = 0; I <m; I ++) {scanf ("% d", & U, & V, & W); edges [I]. U = u; edges [I]. V = V; edges [I]. W = W;} qsort (edges, M, sizeof (edges [0]), CMP); Kruskal (); Return 0 ;}