Cod sursa(job #545164)

Utilizator avram_florinavram florin constantin avram_florin Data 2 martie 2011 20:21:18
Problema Lazy Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include<fstream>
#include<algorithm>

using namespace std;

const int MaxN = 200005;
const char in[] = "lazy.in";
const char out[] = "lazy.out";

int n,m,x[MaxN],y[MaxN],T[MaxN],ind[MaxN];
long long c1[MaxN],c2[MaxN];

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

int find_root(int x)
{
	int y,rad;
	rad = x;
	while( rad != T[rad] )
		rad = T[rad];
	while( x != T[x] )
		{
			y = T[x];
			T[x] = rad;
			x = y;
		}
	return rad;
}

void merge(int x,int y)
{
	T[x] = y;
}

int main()
{
	ifstream f(in);
	ofstream g(out);
	f >> n >> m;
	int i;
	for( i = 1 ; i <= m ; i++ )
		{
			f >> 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;
	for( i = 1 ; i <= m ; i++ )
		if( find_root(x[ind[i]]) != find_root(y[ind[i]]) )
			{
				merge( find_root(x[ind[i]]) , find_root(y[ind[i]]) );
				g << ind[i] << '\n';
			}
	f.close();
	g.close();
	return 0;
}