Cod sursa(job #760406)

Utilizator scipianusFMI Ciprian Olariu scipianus Data 21 iunie 2012 11:38:09
Problema Lazy Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
#include<fstream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
int n,m;
int c[200100],h[200100];
struct Muchie{int x,y,cost,profit,ind;};
Muchie M[200100];
vector <int> sol;

void Citire()
{
	int i;
	ifstream fin("lazy.in");
	fin>>n>>m;
	for(i=1;i<=m;i++)
	{
		fin>>M[i].x>>M[i].y>>M[i].cost>>M[i].profit;
		M[i].ind=i;
	}
	fin.close();
}

inline bool Sortare(Muchie A,Muchie B)
{
	if(A.cost==B.cost)
		return A.profit>B.profit;
	return A.cost<B.cost;
}

inline int Find(int x)
{
	int r=x;
	while(c[r])
		r=c[r];
	int y=x,aux;
	while(y!=r)
	{
		aux=c[y];
		c[y]=r;
		y=aux;
	}
	return r;
}

inline void Union(int x,int y)
{
	if(h[x]>h[y])
		c[y]=x;
	else
	{
		c[x]=y;
		if(h[x]==h[y])
			h[y]++;
	}
}

void Kruskal()
{
	int i=1,A,B,nr=1;
	sort(M+1,M+m+1,Sortare);
	while(nr<n)
	{
		A=Find(M[i].x);
		B=Find(M[i].y);
		
		if(A!=B)
		{
			Union(A,B);
			sol.push_back(M[i].ind);
			nr++;
		}
		i++;
	}
}

void Afisare()
{
	vector <int>::iterator it;
	ofstream fout("lazy.out");
	for(it=sol.begin();it!=sol.end();it++)
		fout<<*it<<"\n";
	fout.close();
}

int main()
{
	Citire();
	Kruskal();
	Afisare();
	return 0;
}