Cod sursa(job #564097)

Utilizator cdascaluDascalu Cristian cdascalu Data 26 martie 2011 18:31:56
Problema Lazy Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include<fstream>
#include<algorithm>
#define MAX 200001
using namespace std;
int N,M,T[MAX];
struct muchie
{
	int x,y,a,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;
	while(cnt<N)
	{
		for(i=last;i<=M;++i)
		{
			if(find(V[i].x)!=find(V[i].y))
			{
				V[i].a = 1;
				unite(T[V[i].x], T[V[i].y]);
				last = i+1;
				i = N+1;
			}
		}
		++cnt;
	}
	ofstream g("lazy.out");
	for(i=1;i<=M;++i)
		if(V[i].a)g<<V[i].i<<"\n";
	g.close();
	return 0;
}