Cod sursa(job #2656179)

Utilizator popoviciAna16Popovici Ana popoviciAna16 Data 6 octombrie 2020 22:48:54
Problema Lazy Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <fstream>
#include <algorithm>
using namespace std;

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

struct abc
{
	int a, b;
	long long c1, c2;
	int ind;
} much[200001];

bool comp(abc a, abc b)
{
	if (a.c1 == b.c1)
		return a.c2 > b.c2;
	return a.c1 < b.c1;
}

bool rasp[200001];

int t[200001], h[200001];

void comb(int x, int y)
{
	if (h[x] < h[y])
		t[x] = y;
	else
	{
		t[y] = x;
		if (h[x] == h[y])
			h[x]++;
	}
}

int find(int x)
{
	int r = x, y;
	while (t[r])
		r = t[r];
	while (x != r)
	{
		y = t[x];
		t[x] = r;
		x = y;
	}
	return r;
}

int main()
{
	int n, m, i;
	int x, y;
	fin >> n >> m;
	for (i = 1; i<=m; i++)
	{
		fin >> much[i].a >> much[i].b >> much[i].c1 >> much[i].c2;
		much[i].ind = i;
	}
	sort(much+1, much+m+1, comp);
	for (i = 1; i<=m; i++)
	{
		x = find(much[i].a);
		y = find(much[i].b);
		if (x != y)
		{
			rasp[much[i].ind] = 1;
			comb(x, y);
		}
	}
	for (i = 1; i<=m; i++)
		if (rasp[i])
			fout << i << '\n';
    return 0;
}