Cod sursa(job #541416)

Utilizator Alexa_ioana_14Antoche Ioana Alexandra Alexa_ioana_14 Data 25 februarie 2011 10:51:40
Problema Lazy Scor 100
Compilator cpp Status done
Runda Romanian Master in Mathematics and Sciences 2011, Ziua 1 Marime 1.08 kb
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
#define LL long long
#define pb push_back
#define NM 200005
LL c1[NM],c2[NM];
int x[NM],y[NM],N,M,ind[NM],gr[NM],rang[NM];
bool cmp(const int &i, const int &j)
{
	if (c1[i]<c1[j])
		return true;
	if (c1[i]==c1[j])
		if (c2[i]>c2[j])
			return true;
	return false;
}
inline void citire()
{
	freopen("lazy.in","r",stdin);
	freopen("lazy.out","w",stdout);
	scanf("%d%d",&N,&M);
	for (int i=1; i<=M; ++i)
	{
		scanf("%d%d%lld%lld",&x[i],&y[i],&c1[i],&c2[i]);
		ind[i]=i;
	}
}
inline int find(int x)
{
	if (gr[x]!=x)
		gr[x]=find(gr[x]);
	return gr[x];
}
inline void unesc(int x, int y)
{
	if (rang[x]<rang[y])
		gr[x]=y;
	else
	{
		rang[x]+=(rang[x]==rang[y]);
		gr[y]=x;
	}
}
inline void apm()
{
	int i,u,v;
	for (i=1; i<=N; ++i)
	{
		gr[i]=i;
		rang[i]=1;
	}
	for (i=1; i<=M; ++i)
	{
		u=find(x[ind[i]]);
		v=find(y[ind[i]]);
		if (u==v)
			continue;
		unesc(u,v);
		printf("%d\n",ind[i]);
	}
}
int main()
{
	citire();
	sort(ind+1,ind+1+M,cmp);
	apm();
	return 0;
}