Cod sursa(job #1684167)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 10 aprilie 2016 20:56:37
Problema Lazy Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.44 kb
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>

using namespace std;

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

const int dim = 200005;

struct Edge {

	int x, y, index;
	long long c1, c2;
	Edge() {}
	Edge(int _x, int _y, int _index, long long _c1, long long _c2) {
		x = _x;
		y = _y;
		index = _index;
		c1 = _c1;
		c2 = _c2;
	}

	bool operator < (const Edge a) const {
		return (c1 == a.c1 ? c2 > a.c2 : c1 < a.c1);
	}

};

vector<Edge> edges;

int disJoint[dim];

int getRoot(int x) {

	int aux = x;

	while (disJoint[x] > 0)
		x = disJoint[x];

	int root = x;
	x = aux;

	while (x != root) {
		aux = disJoint[x];
		disJoint[x] = root;
		x = aux;
	}

	return root;

}

int main() {

	int n, m;
	fin >> n >> m;

	for (int i = 1; i <= n; ++i) {

		int x, y;
		long long c1, c2;

		fin >> x >> y >> c1 >> c2;
		edges.push_back(Edge(x, y, i, c1, c2));

	}

	sort(edges.begin(), edges.end());

	memset(disJoint, -1, sizeof disJoint);

	for (unsigned int i = 0; i < edges.size(); ++i) {

		int x = edges[i].x;
		int y = edges[i].y;

		x = getRoot(x);
		y = getRoot(y);

		if (x == y)
			continue;

		fout << edges[i].index << '\n';

		if (disJoint[x] < disJoint[y]) {
			disJoint[x] += disJoint[y];
			disJoint[y] = x;
		}
		else {
			disJoint[y] += disJoint[x];
			disJoint[x] = y;
		}

	}

	return 0;

}

//Trust me, I'm the Doctor!