Cod sursa(job #2866980)

Utilizator Max_CalincuMaxim Calincu Max_Calincu Data 10 martie 2022 09:35:25
Problema Arbore partial de cost minim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.61 kb
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define pi pair<ll, ll>
#define sz(x) (int)((x).size())
#define all(a) (a).begin(), (a).end()
#define po pair<ll, pi>

/*const ll maxn = 1e5;
int f[maxn],nf[maxn],inv[maxn];
const int M=998244353;
void init(){
    inv[1]=1; for (int i=2;i<maxn;i++) inv[i]=M-1ll*(M/i)*inv[M%i]%M;
    f[0]=nf[0]=1; for (int i=1;i<maxn;i++) f[i]=1ll*f[i-1]*i%M,nf[i]=1ll*nf[i-1]*inv[i]%M;
}
int C(int x,int y){return 1ll*f[x]*nf[y]%M*nf[x-y]%M;} */

const ll mod = 1e9+7;
ll n, k, m, mi, ma;
vector<ll> p, s;

ll get(ll x){
	if(p[x] != x) p[x] = get(p[x]);
	return p[x];
}

void unite(ll x, ll y){
	x = get(x);
	y = get(y);
	if(x == y) return;
	if(s[x] < s[y]) swap(x, y);
	s[x] += s[y];
	p[y] = x;
}

void solve(){
	
	ifstream cin("apm.in");
	ofstream cout("apm.out");
	
	cin >> n >> m;
	for(int i = 0; i<n; i++){
		p.pb(i);
		s.pb(1);
	}
	vector<po> a;
	vector<pi> ans;
	ll rs = 0;
	for(int i = 0; i<m; i++){
		ll x, y, c;
		cin >> x >> y >> c;
		x--; y--;
		a.pb({c, {x, y}});
	}
	sort(all(a));
	for(int i = 0; i<a.size(); i++){
		ll c = a[i].f, x = a[i].s.f, y = a[i].s.s;  
		if(get(x) == get(y)) continue;
		rs += c;
		unite(x, y);
		ans.pb({x, y}); 
	}
	cout << rs << "\n";
	cout << ans.size() << "\n";
	for(int i = 0; i<ans.size(); i++){
		cout << ans[i].f + 1 << " " << ans[i].s + 1 << "\n"; 
	}
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);

	//init();
	
	int t=1;
//	cin >> t;
	while(t--) solve();
	
	return 0;
}