Cod sursa(job #548946)

Utilizator klamathixMihai Calancea klamathix Data 7 martie 2011 22:56:09
Problema Lazy Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.22 kb
#include<cstdio>
#include<fstream>
#define ll long long
using namespace std;

const int maxn = 200005;

ifstream fin ("lazy.in");

int i , j , n , m , dad[maxn] , lev[maxn] , e;

struct muchie {
	ll int  cost , profit;
	int x , y , ind;
} v[maxn];

bool cmp ( const muchie &a , const muchie &b ) {
	if ( a.cost == b.cost )
		return a.profit > b.profit;
	return a.cost < b.cost;
}

int father( int node ) { 
	int root = node , aux;
	
	for( ; root != dad[root] ; ) 
		root = dad[root];
	
	for( ; node != root ; ) { 
		aux = node;
		node = dad[node];
		dad[aux] = root;
	}
return root;
}

void unite ( int A , int B ) {
	if ( lev[A] > lev[B] ) 
		dad[B] = dad[A];
	else
		dad[A] = dad[B];
	
	if ( lev[A] == lev[B] )
		lev[B]++;
}

int main()
{
	freopen("lazy.out","w",stdout);
	
	fin >> n >> m;
	
	for( i = 1 ; i <= m ; ++i ) 
		fin >> v[i].x >> v[i].y >> v[i].cost >> v[i].profit,
		v[i].ind = i;
	
	for( i = 1 ; i <= n ; ++i )
		dad[i] = i , lev[i] = 1;
		
	sort( v + 1 , v + m + 1 , cmp);
	
	for( i = 1 ; i <= m && e < n - 1; ++i ) {
		if ( father(v[i].x) != father(v[i].y) ) {
			unite( father(v[i].x) , father(v[i].y) );
			printf("%d\n",v[i].ind);
			++e;
		}
	}
	
return 0;
}