Cod sursa(job #544084)

Utilizator Addy.Adrian Draghici Addy. Data 1 martie 2011 00:22:58
Problema Lazy Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <cstdio>
#include <algorithm>

using namespace std;

#define NMAX 200050
#define MMAX 200050

#define ll long long

struct muchie {
	int x, y, p; ll c1, c2;
} M[MMAX];

int T[NMAX], n, m, i, x, y;

bool cmp (muchie, muchie), ciclu (int, int);
void kruskal (), uneste (int, int);

int main () {
	
	freopen ("lazy.in", "r", stdin);
	freopen ("lazy.out", "w", stdout);
	
	scanf ("%d %d", &n, &m);
	for (i = 1; i <= m; i++) {
		scanf ("%d %d %lld %lld", &M[i].x, &M[i].y, &M[i].c1, &M[i].c2);
		M[i].c2 = -M[i].c2;
		M[i].p = i;
	}
	
	kruskal ();
	
	return 0;
}

bool cmp (muchie a, muchie b) {
	if (a.c1 == b.c1)
		return a.c2 < b.c2;
	return a.c1 < b.c1;
}

bool ciclu (int x, int y) {
	
	while (T[x] > 0) x = T[x];
	
	while (T[y] > 0) y = T[y];
	
	if (x == y) return 1;
	return 0;
}

void uneste (int x, int y) {
	
	while (T[x] > 0) x = T[x];
	
	while (T[y] > 0) y = T[y];
	
	if (-T[x] > -T[y])
		T[x] += T[y], T[y] = x;
	else
		T[y] += T[x], T[x] = y;
}

void kruskal () {
	
	int i, x, y, apm = 0;
	
	memset (T, -1, sizeof (T));
	
	sort (M + 1, M + 1 + m, cmp);
	
	for (i = 1; i <= m; i++) {
		x = M[i].x, y = M[i].y;
		
		if (!ciclu (x, y)) {
			uneste (x, y);
			apm++; printf ("%d\n", M[i].p);
		}
		
		if (apm == n - 1) return;
	}
}