Cod sursa(job #575095)

Utilizator mathboyDragos-Alin Rotaru mathboy Data 7 aprilie 2011 21:48:10
Problema Lazy Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <cstdio>
#include <algorithm>
#include <vector>

#define maxn 200010
using namespace std;

int t[maxn], rg[maxn], x[maxn], y[maxn], ind[maxn];
long long c1[maxn], c2[maxn];
int n, m;
vector <int> sol;
inline bool cmp (const int &a, const int &b)
{
	if (c1[a] == c1[b])
		return c2[a] > c2[b];
	return c1[a] < c1[b];
}
int group (int x)
{
	if (t[x] == x) return x;
	t[x] = group (t[x]);
	return t[x];
}
void unite (int x, int y)
{
	if (rg[x] < rg[y]) 
		t[group (x)] = group (y);
	else 
		t[group (y)] = group (x);
	if (rg[x] == rg[y]) ++rg[y];
}
int main ()
{


	freopen ("lazy.in", "r", stdin);
	freopen ("lazy.out", "w", stdout);
	
	int i;

	scanf ("%d %d\n", &n, &m);

	for (i = 1; i <= m; i++) {
		scanf ("%d %d %lld %lld\n", &x[i], &y[i], &c1[i], &c2[i]);
		ind[i] = i;
	}

	sort (ind + 1, ind + m + 1, cmp);
	
	for (i = 1; i <= n; i++) t[i] = i, rg[i] = 1;

	for (i = 1; i <= m; i++)
		if (group (x[ind[i]]) != group (y[ind[i]])) {
			sol.push_back (ind[i]);
			unite (x[ind[i]], y[ind[i]]);
		}
	for (i = 0; i < sol.size (); i++)
		printf ("%d\n", sol[i]);

	return 0;
}