Cod sursa(job #541719)

Utilizator ChallengeMurtaza Alexandru Challenge Data 25 februarie 2011 13:33:32
Problema Lazy Scor 100
Compilator cpp Status done
Runda Romanian Master in Mathematics and Sciences 2011, Ziua 1 Marime 1.62 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

const char InFile[]="lazy.in";
const char OutFile[]="lazy.out";
const int MaxN=200111;

ifstream fin(InFile);
ofstream fout(OutFile);

struct s_edge
{
	s_edge(int x2=0,int y2=0,long long tcost1=0, long long tcost2=0, int ind2=0):x(x2),y(y2),ind(ind2),cost1(tcost1),cost2(tcost2){}
	int x,y,ind;
	long long cost1,cost2;
};

long long cost1,cost2;
int N,M,x,y,T[MaxN];
vector<s_edge> e;
vector<int> sol;

inline bool e_cmp(s_edge a, s_edge b)
{
	if(a.cost1!=b.cost1)
	{
		return a.cost1<b.cost1;
	}
	else
	{
		return a.cost2>b.cost2;
	}
}

inline void init()
{
	for(register int i=1;i<=N;++i)
	{
		T[i]=-1;
	}
}

inline int find(int x)
{
	int cx=T[x];
	int lx=x;
	while(cx>0)
	{
		lx=cx;
		cx=T[cx];
	}
	int cx2;cx=x;
	while(cx!=lx)
	{
		cx2=cx;
		cx=T[cx];
		T[cx2]=lx;
	}
	return lx;
}

inline bool unified(int x,int y)
{
	if(find(x)!=find(y))
	{
		return false;
	}
	return true;
}

inline void unify(int x,int y)
{
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy)
	{
		if(T[fx]<T[fy])
		{
			T[fx]+=T[fy];
			T[fy]=fx;
		}
		else
		{
			T[fy]+=T[fx];
			T[fx]=fy;
		}
	}
}

int main()
{
	fin>>N>>M;
	for(register int i=1;i<=M;++i)
	{
		fin>>x>>y>>cost1>>cost2;
		e.push_back(s_edge(x,y,cost1,cost2,i));
	}
	fin.close();
	
	init();
	--N;
	sort(e.begin(),e.end(),e_cmp);
	for(vector<s_edge>::iterator it=e.begin();it!=e.end() && (int)(sol.size())<N;++it)
	{
		if(!unified(it->x,it->y))
		{
			unify(it->x,it->y);
			sol.push_back(it->ind);
		}
	}
	
	for(vector<int>::iterator it=sol.begin();it!=sol.end();++it)
	{
		fout<<*it<<"\n";
	}
	fout.close();
	return 0;
}