Cod sursa(job #2653356)

Utilizator radugnnGone Radu Mihnea radugnn Data 27 septembrie 2020 19:37:07
Problema Lazy Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.22 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("lazy.in");
ofstream fout("lazy.out");
int N, M, i, rx, ry, k;
int t[200010];
int sol[200010];
struct muchie {
    int a;
    int b;
    long long cost1;
    long long cost2;
    int ind;
} f[200010];
bool cmp(muchie x, muchie y) {
    if (x.cost1 == y.cost1)
        return x.cost2 > y.cost2;
    return x.cost1 < y.cost1;
}
int rad(int x) {
    while (t[x] > 0) {
        x = t[x];
    }
    return x;
}
int main() {
    fin >> N >> M;
    for (i = 1; i <= N; i++)
        t[i] = -1;
    for (i = 1; i <= M; i++) {
        fin >> f[i].a >> f[i].b >> f[i].cost1 >> f[i].cost2;
        f[i].ind = i;
       // f[i].cost2 *= f[i].cost1;
    }
    sort(f + 1, f + M + 1, cmp);
    for (i = 1; i <= M; i++) {
        rx = rad(f[i].a);
        ry = rad(f[i].b);
        if (rx != ry) {
            sol[++k] = f[i].ind;

            if (t[rx] < t[ry]) {
                t[rx] += t[ry];
                t[ry] = f[i].a;
            }
            else {
                t[ry] += t[rx];
                t[rx] = ry;
            }
        }
    }

    for (i = 1; i <= k; i++)
        fout << sol[i] << "\n";

    return 0;
}