Cod sursa(job #549783)

Utilizator antoanelaAntoanela Siminiuc antoanela Data 8 martie 2011 22:20:15
Problema Lazy Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <cstdio>
#include <algorithm>
using namespace std;
#define nmax 200010

int gr[nmax], x[nmax], y[nmax], c1[nmax], c2[nmax], sol[nmax], ind[nmax];
int n, m, h;

struct cmp
{
	bool operator() (const int &a, const int &b)const
	{
		if (c1[a]==c1[b]) return c2[a]>c2[b]; else
			return c1[a]<c1[b];
	}
};

int grupa(int i)
{
	if (gr[i]==i) return i;
	gr[i]=grupa(gr[i]);
	return gr[i];
}

void reuniune(int i, int j)
{
	gr[grupa(i)]=grupa(j);
}

int main()
{
	freopen("lazy.in","r",stdin);
	freopen("lazy.out","w",stdout);
	scanf("%d %d",&n, &m);
	int i;
	for (i=1; i<=m; i++)
	{
		scanf("%d %d %d %d", &x[i], &y[i], &c1[i], &c2[i]);
		ind[i]=i;
	}
	for (i=1; i<=n; i++) gr[i]=i;
	sort(ind+1, ind+m+1, cmp());
	for (i=1; i<=m; i++)
		if (grupa(x[ind[i]])!=grupa(y[ind[i]]))
		{
			sol[++h]=ind[i];
			reuniune (x[ind[i]], y[ind[i]]);
		}
	for (i=1; i<=h; i++) printf("%d\n",sol[i]);
}