Cod sursa(job #1240370)

Utilizator paunmatei7FMI Paun Matei paunmatei7 Data 11 octombrie 2014 10:26:09
Problema Lazy Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <cstdio>
#include <vector>
#include <algorithm>

#define NMAX 200007
#define LL long long

using namespace std;

struct lazy{
    int x;
    int y;
    LL c1;
    LL c2;
    int Poz;
};

vector < lazy > a;
int Dad[NMAX], H[NMAX];
int n, m;

lazy make_lazy(int X, int Y, LL C1, LL C2, int POZ){
    lazy d;
    d.x = X;
    d.y = Y;
    d.c1 = C1;
    d.c2 = C2;
    d.Poz = POZ;
    return d;
}

inline int Comp(lazy A, lazy B){
    if(A.c1 != B.c1)
        return A.c1 < B.c1;
    return A.c2 > B.c2;
}

int father(int f){
    if(Dad[f] == f)
        return f;
    return father(Dad[f]);
}

void unite(int x, int y){
    if(H[x] > H[y])
        Dad[y] = x;
    else
        if(H[x] < H[y])
            Dad[x] = y;
        else{
            ++H[x];
            Dad[y] = x;
        }
}

int main(){
    freopen("lazy.in", "r", stdin);
    freopen("lazy.out", "w", stdout);
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= m; ++i){
        int X, Y;
        LL C1, C2;
        scanf("%d %d %lld %lld", &X, &Y, &C1, &C2);
        a.push_back(make_lazy(X, Y, C1, C2, i));
    }
    for(int i = 1; i <= n; ++i){
        Dad[i] = i;
        H[i] = 1;
    }
    sort(a.begin(), a.end(), Comp);
    for(int i = 0; i < m; ++i){
        int Tx = father(a[i].x);
        int Ty = father(a[i].y);
        if(Tx != Ty){
            unite(Tx, Ty);
            printf("%d\n", a[i].Poz);
        }
    }
    return 0;
}