Cod sursa(job #1536727)

Utilizator radu_cebotariRadu Cebotari radu_cebotari Data 26 noiembrie 2015 16:32:19
Problema Lazy Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.34 kb
#include<fstream>
#include<algorithm>
#include<iostream>
using namespace std;
ifstream in("lazy.in");
ofstream out("lazy.out");
const int NMAX = 200005;

struct muchie{

    int x,y,ind;
    long long c1,c2;
};
muchie v[NMAX];

int N,M,T[NMAX],R[NMAX];

bool cmp(muchie m1,muchie m2)
{

    if(m1.c1 == m2.c1)
        return m1.c2 > m2.c2;
    return m1.c1 < m2.c1;
}

int tata(int nod)
{

    int R;
    for(R = nod ; R != T[R] ; R = T[R]);
    int y;
    for( ; T[nod] != nod ; ){

        y = T[nod];
        T[nod] = R;
        nod = y;
    }
    return R;
}

void unire(int x,int y)
{

    if(R[x] > R[y])
        T[y] = T[x];
    else
        T[x] = T[y];
    if(R[x] == R[y])
        ++R[y];

}

void read()
{

    in>>N>>M;
    for(int i = 1 ; i <= M ; ++i){

        in>>v[i].x>>v[i].y>>v[i].c1>>v[i].c2;
        v[i].ind = i;
    }
    for(int i = 1 ; i <= N ; ++i){
        T[i] = i;
        R[i] = 1;
    }
    in.close();
}

void solve()
{

    sort(v+1 , v + N + 1 , cmp);
    int mc = 0,j = 1;
    while(mc != N-1){

        if(tata(v[j].x) != tata(v[j].y)){

            unire(tata(v[j].x),tata(v[j].y));
            out<<v[j].ind<<"\n";
            ++mc;
        }
        ++j;
    }
    out.close();
}

int main()
{

    read();
    solve();
    return 0;
}