Cod sursa(job #2881476)

Utilizator QwertyDvorakQwerty Dvorak QwertyDvorak Data 30 martie 2022 15:28:39
Problema Arbore partial de cost minim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define dbg(x) cout << #x <<": " << x << "\n";
#define sz(x) ((int)x.size())

using ll = long long;

const string fn = "apm";
ifstream fin(fn + ".in");
ofstream fout(fn + ".out");

int n, m;
int t[200005];

void unionn(int x, int y) {
	t[y] = x;
}

int find(int x) {
	return (t[x] == x ? x : t[x] = find(t[x]));
}

vector<pair<int, pair<int , int > > > v;
int main() {

	fin >> n >> m;
	for (int i = 1; i <= n; ++i)
		t[i] = i;
	while (m--) {
		int x, y, w;
		fin >> x >> y >> w;
		v.pb({w, {x, y}});
	}

	sort(v.begin(), v.end());
	int cost = 0;
	vector<pair<int , int> > ans;
	for (int i = 0; i < sz(v); ++i) {
		int x, y;
		x = find(v[i].second.first);
		y = find(v[i].second.second);
		if (x != y) {
			unionn(x, y);
			cost += v[i].first;
			ans.pb({x, y});
		}
	}

	fout << cost << "\n";
	fout << sz(ans) << '\n';
	for (auto i : ans)
		fout << i.first << " " << i.second << '\n';

	return 0;
}