Cod sursa(job #2693918)

Utilizator Andrei21AAnea Andrei Andrei21A Data 7 ianuarie 2021 16:25:15
Problema Lazy Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

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

const int nmax = 200005;
int t[nmax], sol[nmax], top, n, m;

struct sosea
{
	int x, y, poz;
	long long c1, c2;
};

sosea a[nmax];

void Union(int x, int y)
{
	t[x] = y;
}

int Find(int x)
{
	int rad = x, aux;
	while (t[rad])
	{
		rad = t[rad];
	}
	while (t[x])
	{
		aux = t[x];
		t[x] = rad;
		x = aux;
	}
	return rad;
}

int cmp(const sosea A, const sosea B)
{
	if (A.c1 == B.c1)return A.c2 > B.c2;
	return A.c1 < B.c1;
}

void Citire()
{
	fin >> n >> m;
	for (int i = 1; i <= m; i++)
	{
		fin >> a[i].x >> a[i].y >> a[i].c1 >> a[i].c2;
		a[i].poz = i;
	}
	sort(a + 1, a + m + 1, cmp);
}

void Rezolvare()
{
	int v, w,nrc = n;
	for (int i = 1; i <= m and nrc > 1; i++)
	{
		v = a[i].x;
		w = a[i].y;
		v = Find(v);
		w = Find(w);
		if (v != w)
		{
			Union(v, w);
			nrc--;
			sol[++top] = a[i].poz;
		}
	}
	for (int i = 1; i <= top; i++)
		fout << sol[i] << " ";
}

int main()
{
	Citire();
	Rezolvare();
	return 0;
}