Cod sursa(job #573684)

Utilizator bog29Antohi Bogdan bog29 Data 6 aprilie 2011 14:53:07
Problema Lazy Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.09 kb
#include<fstream>
#include<algorithm>
#include<vector>
#define dmax 200003
using namespace std;
ifstream in("lazy.in");
ofstream out("lazy.out");

int n,m,f[dmax],h[dmax];

struct muchie
{	
	int a;
	int b;
	long long c;
	long long d;
	int nr;
}	mc[dmax];	


int sf(muchie x, muchie y)
{	
	if(x.c != y.c)
		return x.c < y.c;
	
	return x.d > y.d;
}

int bun(int x, int y)
{	
	while(f[x])
		x = f[x];
	
	while(f[y])
		y = f[y];

	if(x == y)
		return 0;
	return 1;
}

void uneste(int x, int y)
{	
	while(f[x])
		x = f[x];
	
	while(f[y])
		y = f[y];
	
	if(h[x] < h[y])
		f[x] = y;
	
	if(h[x] > h[y])
		f[y] = x;
	
	if(h[x] == h[y])
	{	f[y] = x;
		h[x]++;
	}	
}


void kruskal()
{	
	int k=0,i;
	
	for(i=1; k<n-1; i++)
		if(bun(mc[i].a, mc[i].b) )
		{	out<<mc[i].nr<<'\n';
			k++;
			uneste(mc[i].a, mc[i].b);
		}	
}





int main()
{	
	int i;
	
	in>>n>>m;
	
	for(i=1;i<=m;i++)
	{	in>>mc[i].a>>mc[i].b>>mc[i].c>>mc[i].d;
		mc[i].nr = i;
	}	
	
	in.close();

	sort( mc+1, mc+m+1, sf);
	
	kruskal();
	
	out.close();
	return 0;
}