Cod sursa(job #387096)

Utilizator bixcabc abc bixc Data 26 ianuarie 2010 20:27:42
Problema Arbore partial de cost minim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <cstdio>
#include <algorithm>
using namespace std;

#define Nmax 200010
#define Mmax 400010

struct muchie {int x, y, cst;} M[Mmax];
void citire (), afisare ();
int n, m, cst;
int T[Nmax], sol[Nmax];

int cmp (muchie a, muchie b) {
	return a.cst < b.cst;
}

int tata (int nod) {
	
	while (T[nod] > 0)
		nod = T[nod];
	
	return nod;
}

void apm () {
	
	memset (T, -1, sizeof (T));
	sort (M + 1, M + m + 1, cmp);
	
	int t1, t2;
	for (int i = 1; i <= m; i++) {
		t1 = tata (M[i].x);
		t2 = tata (M[i].y);
		
		if (t1 != t2) {
			if (-T[t1] > - T[t2]) {
				T[t1]+= T[t2];
				T[t2] = t1;
			}
			
			else {
				T[t2]+= T[t1];
				T[t1] = t2;
			}
			
			cst+= M[i].cst;
			sol[++sol[0]] = i;
		}
	}
}

int main () {

	freopen ("apm.in", "r", stdin);
	freopen ("apm.out", "w", stdout);

	citire ();
	apm ();
	afisare ();
	
	return 0;
}

void citire () {

	scanf ("%d %d", &n, &m);
	for (int i = 1; i <= m; i++) 
		scanf ("%d %d %d", &M[i].x, &M[i].y, &M[i].cst);
}

void afisare () {
	
	printf ("%d\n%d\n", cst, sol[0]);
	for (int i = 1; i <= sol[0]; i++)
		printf ("%d %d\n", M[sol[i]].x, M[sol[i]].y);
}