Cod sursa(job #557953)

Utilizator rares192Preda Rares Mihai rares192 Data 16 martie 2011 23:30:05
Problema Lazy Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 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;
};

muchie a[200001];

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

bool pred(muchie a, muchie b)
{
	return a.c1 < b.c1 || ( a.c1 == b.c1 && a.c2 > b.c2 ); 
}

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

void Read()
{
	ifstream fin("lazy.in");
	fin >> n >> m;
	

	for(int i = 1; i <= m; ++i)
	{
		if( i <= n)
			t[i] = i;
		
		fin >> a[i].x >> a[i].y >> a[i].c1 >> a[i].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; ++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);
		}
		
		if( k == n - 1)
			return;
	}
	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]++;
	}
}