Cod sursa(job #564101)

Utilizator cdascaluDascalu Cristian cdascalu Data 26 martie 2011 18:40:57
Problema Lazy Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include<fstream>
#include<algorithm>
#define MAX 200001
using namespace std;
int N,M,T[MAX];
struct muchie
{
	int x,y,i;
	long long c1,c2;
}V[MAX];
int cmp(muchie a, muchie b)
{
	if(a.c1 == b.c1)return (a.c2>b.c2);
	return (a.c1<b.c1);
}
void read()
{
	ifstream f("lazy.in");
	f>>N>>M;
	int i;
	for(i=1;i<=M;++i)
	{
		f>>V[i].x>>V[i].y>>V[i].c1>>V[i].c2;
		V[i].i = T[i] = i;
	}
	f.close();
}
void unite(int x,int y)
{
	T[y] = x;
}
void compress(int x,int father)
{
	int aux;
	while(x!=T[x])
	{
		aux = T[x];
		T[x] = father;
		x = aux;
	}
}
int find(int x)
{
	int init = x;
	while(x!=T[x])
		x = T[x];
	compress(init,x);
	return x;
}
int main()
{
	read();
	sort(V+1,V+1+M,cmp);
	int cnt=1,last=1,i,sol[MAX];
	for(i=1;i<=M && sol[0]!=N-1;++i)
	{
		if(find(V[i].x)!=find(V[i].y))
		{
			sol[++sol[0]] = V[i].i;
			unite(T[V[i].x], T[V[i].y]);
		}
	}
	ofstream g("lazy.out");
	for(i=1;i<N;++i)
		g<<sol[i]<<"\n";
	g.close();
	return 0;
}