Cod sursa(job #541791)

Utilizator Addy.Adrian Draghici Addy. Data 25 februarie 2011 14:24:53
Problema Lazy Scor 0
Compilator cpp Status done
Runda Romanian Master in Mathematics and Sciences 2011, Ziua 1 Marime 1.34 kb
#include <cstdio>
#include <algorithm>

using namespace std;

#define NMAX 200050
#define MMAX 200050

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

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

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

int 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, m_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;
		
		while (!ciclu (x, y) && M[i].c1 == M[i+1].c1 && i < n) {
			x = M[i].x, y = M[i].y;
			i++;
		}
		x = M[i].x, y = M[i].y;
		
		if (!ciclu (x, y)) {
			uneste (x, y);
			m_apm++;
			printf ("%d\n", M[i].p);
		}
		
		if (m_apm == n - 1)
			return;
	}
}

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