Cod sursa(job #2937488)

Utilizator DKMKDMatei Filibiu DKMKD Data 10 noiembrie 2022 15:57:21
Problema Lazy Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <bits/stdc++.h>
#define N 200005

using namespace std;

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

int n, m;
vector<int>t;
struct edge {
	int x, y, ind;
	long long o, p;
	bool operator <(const edge A) const {
		if (o == A.o)
			return p > A.p;
		return o < A.o;
	}
}g[N];
void read() {
	fin >> n >> m;
	t.resize(n + 5);
	for (int i = 1; i <= m; ++i) {
		fin >> g[i].x >> g[i].y >> g[i].o >> g[i].p;
		g[i].ind = i;
	}
}
int Find(int x) {
	int root = x,y;
	while (t[root])
		root = t[root];
	while (x != root) {
		y = t[x];
		t[x] = root;
		x = y;
	}
	return root;
}
void Union(int x, int y) {
	t[y] = x;
}
void solve() {
	sort(g + 1, g + m + 1);
	for (int i = 1; i <= m; ++i) {
		int r1 = Find(g[i].x), r2 = Find(g[i].y);
		if (r1 != r2) {
			Union(r1, r2);
			fout << g[i].ind << "\n";
		}
	}
}
int main() {
	read();
	solve();
	return 0;
}