Cod sursa(job #708106)

Utilizator deneoAdrian Craciun deneo Data 6 martie 2012 13:48:55
Problema Lazy Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <fstream>
#include <algorithm>
using namespace std;

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

struct muchie {
    long long x, y, cost, profit, ind;
}v[200010];

int n, m, arb[200010], r[200010];

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

int getRad(int a) {
    if(arb[a] == a)
        return a;
    arb[a] = getRad(arb[a]);
    return arb[a];
}

void join(int a, int b) {
    getRad(a); getRad(b);
    arb[b] = arb[a];
}

int main() {
    int i, j;
    fin >> n >> m;
    for(i = 1; i <= m; ++i) {
        fin >> v[i].x >> v[i].y >> v[i].cost >> v[i].profit;
        v[i].ind = i;
    }
    sort(v + 1, v + m + 1, cmp);
    for(i = 1; i <= n; ++i)
        arb[i] = i;
    for(i = 1; i <= m; ++i) {
        if(getRad(v[i].x) != getRad(v[i].y)) {
            join(v[i].x, v[i].y);
            r[++r[0]] = v[i].ind;
        }
    }
    for(i = 1; i <= r[0]; ++i)
        fout << r[i] << "\n";
    fout.close();
    return 0;
}