#include #include #include #include #include #include #include using namespace std; const int maxn = 1e5 + 10; int idx[maxn], sz[maxn]; int find_root(int x) { while(x != idx[x]) { idx[x] = idx[idx[x]]; x = idx[x]; } return x; } bool check(int a, int b) { if(find_root(a) == find_root(b)) { return true; } return false; } void unite(int a, int b) { int a_root = find_root(a); int b_root = find_root(b); if(a_root != b_root) { if(sz[a_root] < sz[b_root]) { idx[a_root] = idx[b_root]; sz[b_root] += sz[a_root]; } else { idx[b_root] = idx[a_root]; sz[a_root] += sz[b_root]; } } } int main() { for(int i = 0; i < maxn; i++) { idx[i] = i; sz[i] = 1; } int n, m; cin >> n >> m; vector > > graph; for(int i = 0; i < m; i++) { int a, b, c; cin >> a >> b >> c; graph.push_back(make_pair(c, make_pair(a, b))); } sort(graph.begin(), graph.end()); int MST = 0; for(int i = 0; i < m; i++) { int a = graph[i].second.first; int b = graph[i].second.second; int c = graph[i].first; if(!check(a, b)) { MST += c; cout << a << " " << b << endl; unite(a, b); } } cout << MST << endl; return 0; }