Cod sursa(job #540841)

Utilizator wefgefAndrei Grigorean wefgef Data 24 februarie 2011 15:18:31
Problema Lazy Scor Ascuns
Compilator cpp Status done
Runda Marime 1.43 kb
#include <cstdlib>

#include <algorithm>
#include <fstream>
#include <vector>
using namespace std;


const int MAX_N = 200000;

struct Edge {
    int a, b;
    long long c1, c2;
    int index;

    Edge(int a_, int b_, long long c1_, long long c2_, int index_) :
        a(a_), b(b_), c1(c1_), c2(c2_), index(index_) {}

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


int n, m;
vector<Edge> edges;
int father[MAX_N + 1];


void read() {
    ifstream fin("lazy.in");
    fin >> n >> m;
    for (int i = 1; i <= m; ++i) {
        int a, b;
        long long c1, c2;

        fin >> a >> b >> c1 >> c2;
        edges.push_back(Edge(a, b, c1, c2, i));
    }
}

int find(int node) {
    if (father[node] == node) return node;
    return father[node] = find(father[node]);
}

void merge(int a, int b) {
    if (rand() & 1) father[a] = b;
    else father[b] = a;
}

void solve() {
    vector<int> sol;
    for (int i = 1; i <= n; ++i)
        father[i] = i;
    sort(edges.begin(), edges.end());
    for (vector<Edge>::iterator it = edges.begin(); it != edges.end(); ++it)
        if (find(it->a) != find(it->b)) {
            merge(find(it->a), find(it->b));
            sol.push_back(it->index);
        }

     // write
     ofstream fout("lazy.out");
     for (vector<int>::iterator it = sol.begin(); it != sol.end(); ++it)
         fout << *it << '\n';
}

int main() {
    read();
    solve();
}