#include #include #include #include #include #include using namespace std; const int maxn =305; int idx[maxn], sz[maxn]; int num_of_union; void reset_val() { for(int i = 0; i < maxn; i++) { sz[i] = 1; idx[i] = i; } num_of_union = 0; } int find_root(int x) { while(idx[x] != x) { idx[x] = idx[idx[x]]; x = idx[x]; } return x; } bool is_in_the_same_union(int a, int b) { if(find_root(a) == find_root(b)) { return true; } return false; } void union_two_elems(int a, int b) { int root_a = find_root(a); int root_b = find_root(b); if(root_a != root_b) { num_of_union++; if(sz[root_a] < sz[root_b]) { sz[root_b] += sz[root_a]; idx[root_a] = idx[root_b]; } else { sz[root_a] += sz[root_b]; idx[root_b] = idx[root_a]; } } } int main() { 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; reset_val(); vector > v; for(int i = 0; i < m; i++) { int c = graph[i].first; int a = graph[i].second.first; int b = graph[i].second.second; if(!is_in_the_same_union(a, b)) { union_two_elems(a, b); v.push_back(make_pair(a, b)); MST += c; } } int M2 = 2e9; for(int i = 0; i < v.size(); i++) { int a1 = v[i].first, b1 = v[i].second; int mst2 = 0; reset_val(); for(int j =0 ; j < m; j++) { if(graph[j].second.first == a1 and graph[j].second.second == b1) continue; int c = graph[j].first; int a = graph[j].second.first; int b = graph[j].second.second; if(!is_in_the_same_union(a, b)) { union_two_elems(a, b); mst2 += c; } } if(num_of_union == n - 1) { M2 = min(M2, mst2); } } cout << MST << endl << M2 << endl; return 0; }