Cod sursa(job #2850904)

Utilizator cristia_razvanCristia Razvan cristia_razvan Data 17 februarie 2022 19:00:51
Problema Lazy Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
using ll = long long;


const string fn = "lazy";

ifstream fin(fn + ".in");
ofstream fout(fn + ".out");


int n, m;
int t[200005];


struct edge {
    int x, y, i;
    ll cost, profit;

    bool operator < (edge const &oth) {
        if (cost != oth.cost)
            return (cost < oth.cost);
        return (profit > oth.profit);
    }
};

int Find(int x) {

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

}


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


vector<edge> g;

int main() {

    int x, y, cost, profit;

    fin >> n >> m;
    g.resize(m + 1);
    for (int i = 1; i <= m; ++i) {
        fin >> g[i].x >> g[i].y >> g[i].cost >> g[i].profit;
        g[i].i = i;
    }

    sort(g.begin() + 1, g.end());

    for (int i = 1; i <= m; ++i) {
        x = Find(g[i].x);
        y = Find(g[i].y);
        if (x != y) {
            Union(x, y);
            fout << g[i].i << '\n';
        }
    }

    fin.close();
    fout.close();
    return 0;
}