Cod sursa(job #556226)

Utilizator CezarMocanCezar Mocan CezarMocan Data 16 martie 2011 00:05:15
Problema Lazy Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.57 kb
#include <cstdio>
#include <algorithm>
#include <math.h>

#define ff first
#define ss second
#define ll long long

using namespace std;

const int maxN = 200100;

int N, M;
pair <pair <ll, ll>, int> muc[maxN];
pair <int, int> V[maxN];
int P[maxN];

inline int tata(int x) {
	if (P[x] == x)
		return x;

	P[x] = tata(P[x]);
	return P[x];
}

inline bool cmp(pair <pair <ll, ll>, int> A, pair <pair <ll, ll>, int> B) {
	if (A.ff.ff < B.ff.ff)
		return true;
	if (A.ff.ff > B.ff.ff)
		return false;

	long double p1 = A.ff.ff * 1.0, q1 = A.ff.ss * 1.0, p2 = B.ff.ff * 1.0, q2 = B.ff.ss * 1.0;
	if (q1 > 0 && q2 > 0) {
		if (log(p1) + log(q1) > log(p2) + log(q2))
			return true;
		else
			return false;
	}

	if (q1 > 0 && q2 < 0)
		return true;
	if (q1 < 0 && q2 > 0)
		return false;

	if (q1 < 0 && q2 < 0) {
		if (log(p1) + log(-q1) > log(p2) + log(-q2))
			return false;
		else
			return true;
	}

	if (q1 == 0 && q2 > 0)
		return false;
	if (q1 == 0 && q2 < 0)
		return true;
	if (q1 < 0 && q2 == 0)
		return false;
	if (q1 > 0 && q2 == 0)
		return true;
	return true;
}

int main() {
	int i, j;
	int a, b;
	long long c, d;

	freopen("lazy.in", "r", stdin);
	freopen("lazy.out", "w", stdout);

	scanf("%d%d", &N, &M);

	for (i = 1; i <= M; i++) {
		scanf("%d%d%lld%lld", &a, &b, &c, &d);
		V[i] = make_pair(a, b);
		muc[i] = make_pair(make_pair(c, d), i);
	}

	sort(muc + 1, muc + M + 1, cmp);

	for (i = 1; i <= N; i++)
		P[i] = i;

	for (i = 1; i <= M; i++) {
		int t = muc[i].ss;
		if (tata(V[t].ff) != tata(V[t].ss)) {
			P[tata(V[t].ff)] = tata(V[t].ss);
			printf("%d\n", t);
		}
	}


	return 0;
}