Cod sursa(job #557930)

Utilizator rares192Preda Rares Mihai rares192 Data 16 martie 2011 23:07:41
Problema Lazy Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
#include<fstream>
#include<algorithm>
#include<vector>
using namespace std;

void Read();
void Kruskall();
void Union(int, int);
int Find(int );

struct muchie
{
	int x, y, nr;
	long long c1, c2;
} a[200003];

int n, m, k;
int t[200003], h[200003];

bool pred(const muchie &x, const muchie &y)
{
	return ( (x.c1 < y.c1 ) || (x.c1 == y.c1 && x.c2 > y.c2 ) );
}

int main()
{
	Read();
	Kruskall();
	return 0;
}

void Read()
{
	ifstream fin("lazy.in");
	fin >> n >> m;
	
	int x, y, c1, c2;
	for(int i = 1; i <= m; ++i)
	{
		if( i <= n)
			t[i] = i;
		
		fin >> x >> y >> c1 >> c2;
		a[i].x = x;
		a[i].y = y;
		a[i].c1 = c1;
		a[i].c2 = c2;
		a[i].nr = i;
	}
	
	fin.close();
}

void Kruskall()
{
	ofstream fout("lazy.out");
	
	sort(a + 1, a + m + 1, pred);
	
	for(int i = 1; i <= m && k <= n - 1; ++i)
	{
		int v1 = Find( a[i].x );
		int v2 = Find( a[i].y );
		
		if( v1 != v2)
		{
			++k;
			fout << a[i].nr << '\n';
			Union(v1, v2);
		}
	}
	fout.close();
}

int Find(int x)
{
	if( x != t[x] )
		t[x] = Find( t[x] );
	return t[x];
}

void Union(int x, int y)
{
	if( h[x] > h[y] )
		t[y] = x;
	else
	{
		t[x] = y;
		if( h[x] == h[y] )
			++h[y];
	}
}