Cod sursa(job #857411)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 17 ianuarie 2013 20:05:35
Problema Lazy Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.69 kb
#include <cstdio>
#include <cassert>
#include <algorithm>
#include <vector>

using namespace std;

typedef long long int64;

const int MaxN = 200005;

struct Edge {
    int index, x, y;
    int64 c1, c2;

    Edge() {}

    Edge(const int index, const int x, const int y, const int64 c1, const int64 c2) {
        this->index = index, this->x = x, this->y = y;
        this->c1 = c1, this->c2 = c2;
    }

    bool operator <(const Edge &other) const {
        if (c1 != other.c1)
            return c1 < other.c1;
        return c2 > other.c2;
    }
};

vector<Edge> E;
int N, Father[MaxN];
vector<int> MST;

inline int Find(int X) {
    int Root;
    for (Root = X; Father[Root] != 0; Root = Father[Root]);
    for (int Y; X != Root; X = Y) {
        Y = Father[X]; Father[X] = Root;
    }
    return Root;
}

inline bool Merge(int X, int Y) {
    X = Find(X), Y = Find(Y);
    if (X == Y)
        return false;
    Father[Y] = X;
    return true;
}

void Kruskal() {
    sort(E.begin(), E.end());
    for (size_t i = 0; i < E.size(); ++i)
        if (Merge(E[i].x, E[i].y))
            MST.push_back(E[i].index);
}

void Read() {
    assert(freopen("lazy.in", "r", stdin));
    int M; assert(scanf("%d %d", &N, &M) == 2);
    for (int i = 1; i <= M; ++i) {
        int x, y; int64 c1, c2;
        assert(scanf("%d %d %lld %lld", &x, &y, &c1, &c2) == 4);
        E.push_back(Edge(i, x, y, c1, c2));
    }
}

void Print() {
    assert(freopen("lazy.out", "w", stdout));
    sort(MST.begin(), MST.end());
    for (size_t i = 0; i < MST.size(); ++i)
        printf("%d\n", MST[i]);
}

int main() {
    Read();
    Kruskal();
    Print();
    return 0;
}