Cod sursa(job #2653323)

Utilizator radugheoRadu Mihai Gheorghe radugheo Data 27 septembrie 2020 18:19:49
Problema Lazy Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.22 kb
#include <bits/stdc++.h>

using namespace std;

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

struct muchie{
    long long x, y, cost, profit, indice;
};

bool cmp (muchie a, muchie b){
    if (a.cost != b.cost){
        return a.cost < b.cost;
    }
    else{
        return a.profit > b.profit;
    }
}

muchie v[200005];

int n, m, i, radx, rady, k;
int t[200005], apm[200005];

int radacina (int a){
    while (t[a] > 0){
        a = t[a];
    }
    return a;
}

int main(){
    fin >> n >> m;
    for (i=1; i<=m; i++){
        fin >> v[i].x >> v[i].y >> v[i].cost >> v[i].profit;
        v[i].indice = i;
    }
    sort (v + 1, v + m + 1, cmp);
    for (i=1; i<=n; i++){
        t[i] = -1;
    }
    for (i=1; i<=m; i++){
        radx = radacina (v[i].x);
        rady = radacina (v[i].y);
        if (radx == rady){
            continue; //ac comp conexa
        }
        apm[++k] = v[i].indice;
        if (t[radx] < t[rady]){
            t[radx] += t[rady];
            t[rady] = radx;
        }
        else{
            t[rady] += t[radx];
            t[radx] = rady;
        }
    }
    for (i=1; i<=k; i++){
        fout << apm[i] << "\n";
    }
    return 0;
}