Cod sursa(job #546382)

Utilizator mihaipopa12Popa Mihai mihaipopa12 Data 4 martie 2011 20:56:05
Problema Lazy Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.18 kb
#include<stdio.h>
#include<algorithm>
using namespace std;
#define maxN 200005
#define i64 long long
FILE*f=fopen("lazy.in","r");
FILE*g=fopen("lazy.out","w");

int N,M,i,T[maxN],k,R[maxN];
i64 Rez;

struct mch{
	int x;
	int y;
	int pozinit;
	i64 C1;
	i64 C2;
}A[maxN];

int cmp(mch a,mch b){
	if ( a.C1 != b.C1 )
		return a.C1 < b.C1;
	return a.C2 > b.C2;
}

int root(int x){
	int R;
	for ( R = x ; T[R] != R ; R = T[R] );
	int y;
	for ( ; T[x] != x ; ){
		y = T[x];
		T[x] = R;
		x = y;
	}
	return R;
}

void reun( int nod1 , int nod2 ){
	int ra = root(nod1);
	int rb = root(nod2);
	if ( R[ra] > R[rb] ){
		T[rb] = ra;
	}
	else{
		T[ra] = rb;
	}
	if ( R[ra] == R[rb] )	++R[rb];
}

int main () {
	fscanf(f,"%d %d",&N,&M);
	for ( i = 1 ; i <= M ; ++i ){
		fscanf(f,"%d %d %lld %lld",&A[i].x,&A[i].y,&A[i].C1,&A[i].C2);
		A[i].pozinit = i;
	}
	
	sort(A+1,A+M+1,cmp);
	
	for ( i = 1 ; i <= N ; ++i ){
		T[i] = i;
		R[i] = 1;
	}
	
	for ( i = 1 ; i <= M && k < N - 1 ; ++i ){
		if ( root( A[i].x ) != root( A[i].y ) ){
			++k;
			reun( A[i].x , A[i].y );
			fprintf(g,"%d\n",A[i].pozinit);
		}
	}
	
	
	fclose(f);
	fclose(g);
	
	return 0;
}