Cod sursa(job #761894)

Utilizator vendettaSalajan Razvan vendetta Data 27 iunie 2012 19:58:46
Problema Lazy Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.23 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

#define nmax 200004

ifstream f("lazy.in");
ofstream g("lazy.out");

int n, m, rez[nmax];
typedef struct{
    int x, y, c1, c2, poz;
}camp;
camp v[nmax];
int t[nmax];

bool cmp(camp a, camp b){

    if (a.c1 == b.c2){
        return a.c2 > b.c2;
    }

    return a.c1 < b.c1;

}

void citeste(){

    f >> n >> m;

    for(int i=1; i<=m; i++){
        f >> v[i].x >> v[i].y >> v[i].c1 >> v[i].c2;
        v[i].poz = i;
    }

    sort(v+1, v+m+1, cmp);

}

inline int radacina(int x){

    int cpy = x;

    for(;t[x]!=0;x=t[x]);

    for(;cpy!=x;){
        int temp = cpy;
        t[cpy] = x;
        cpy = t[temp];
    }

    return x;

}

void uneste(int x, int y){

    t[x] = y;

}

void rezolva(){

    for(int i=1; i<=m; i++){
        int X = radacina(v[i].x);
        int Y = radacina(v[i].y);
        if (X == Y) continue;
        uneste(X,Y);
        rez[++rez[0]] = v[i].poz;
    }

    //for(int i=1; i<n; i++) cout << rez[i] << "\n";
    for(int i=1; i<n; i++) g << rez[i] << "\n";

}

int main(){

    citeste();
    rezolva();

    f.close();
    g.close();

    return 0;

}