Cod sursa(job #604691)

Utilizator mottyMatei-Dan Epure motty Data 24 iulie 2011 16:15:14
Problema Lazy Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <fstream>
#include <algorithm>

using namespace std;

ifstream in("lazy.in");
ofstream out("lazy.out");

struct edge{
	int a, b;
	int c, d;
	int id;
	bool on;
};

const int N = 200002;

int n, m;
int rank[N];
int root[N];
edge v[N];

void Read()
{
	in >> n >> m;

	for (int i = 1; i <= m; ++i)
	{
		in >> v[i].a >> v[i].b >> v[i].c >> v[i].d;
		v[i].id = i;
	}
}

int Compare(edge x, edge y)
{
	if (x.c == y.c)
		return (x.d < y.d);

	return (x.c < y.c);
}

void Initialize()
{
	for (int i = 1; i <= n; ++i)
		rank[i] = 1, root[i] = i;
}

inline int GetRoot(int node)
{
	if (node == root[node])
		return node;

	root[node] = GetRoot(root[node]);

	return root[node];
}

void Solve()
{
	int eLeft = n - 1;

	for (int i = 1; i <= m && eLeft; ++i)
	{
		int a = GetRoot(v[i].a);
		int b = GetRoot(v[i].b);

		if (a != b)
		{
			if (rank[a] < rank[b])
				a ^= b ^= a ^= b;

			rank[a] += rank[b];
			root[b] = a;
			v[i].on = true;
			eLeft--;
		}
	}
}

int IdCompare(edge x, edge y)
{
	return (x.id < y.id);
}

void Print()
{
	int eLeft = n - 1;

	for (int i = 1; i <= m && eLeft; ++i)
		if (v[i].on)
		{
			out << i << "\n";
			eLeft--;
		}
}

int main()
{
	Read();
	sort(v + 1, v + n + 1, Compare);
	Initialize();
	Solve();
	sort(v + 1, v + n + 1, IdCompare);
	Print();

	return 0;
}