Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <set>
- #include <cstring>
- #include <queue>
- #include <algorithm>
- using namespace std;
- const int maxn = 300;
- int idx[maxn], sz[maxn];
- void init_values() {
- for(int i = 0; i < maxn; i++) {
- idx[i] = i;
- sz[i] = 1;
- }
- }
- 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 root_a = find_root(A);
- int root_b = find_root(B);
- if(root_a != root_b) {
- 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() {
- init_values();
- int n, m;
- cin >> n >> m;
- vector<pair<int, pair<int, int> > > 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 MST1 = 0;
- vector<pair<int, int> > mst;
- 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)) {
- unite(a, b);
- MST1 += c;
- mst.push_back(make_pair(a, b));
- }
- }
- int MST2 = 2e9;
- for(int i = 0; i < mst.size(); i++) {
- init_values();
- int cnt = 0;
- int tmp_mst = 0;
- for(int j = 0; j < m; j++) {
- int a = graph[j].second.first;
- int b = graph[j].second.second;
- int c = graph[j].first;
- if(a == mst[i].first and b == mst[i].second) continue;
- if(!check(a, b)) {
- unite(a, b);
- tmp_mst += c;
- cnt++;
- }
- }
- if(cnt == n - 1) {
- MST2 = min(MST2, tmp_mst);
- }
- }
- cout << MST1 << " " << MST2 << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement