Cod sursa(job #987316)

Utilizator mitrutstrutMitrea Andrei Ionut mitrutstrut Data 20 august 2013 13:51:45
Problema Lazy Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.75 kb
#include <fstream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
 
typedef long long int64;
 
ifstream fin ("lazy.in");
ofstream fout ("lazy.out");
 
const int Nmax = 200009;
 
int N; int M; int T[Nmax]; int R[Nmax];
 
bool Take[Nmax]; vector <int> Result;
 
 
struct Edge {
 
    int X;int Y; int64 C1; int64 C2; int Ind;
 
    Edge(int X, int Y, int64 C1, int64 C2, int Ind) {
        this -> Y = Y; this -> X = X;
        this -> C1 = C1; this -> C2 = C2;
        this -> Ind = Ind;
    }
 
    bool operator < (const Edge X) const {
        if(this -> C1 == X.C1)
            return this -> C2 > X.C2;
        return this -> C1 < X.C1;
    }
};
 
vector <Edge> E;
 
void Read() {
 
    fin >> N >> M;
    for(int i = 1; i <= M; ++i){
 
        int X; int Y; int64 C1; int64 C2;
        fin >> X >> Y >> C1 >> C2;
        E.push_back(Edge(X, Y, C1, C2, i));
    }
}
 
 
int Find(int X){
 
    return (X == T[X]) ? X : Find(T[X]);
 
}
 
 
void Union(int X, int Y) {
 
    if(R[X] == R[Y])
        T[X] = Y, R[Y]++;
 
    if(R[X] > R[Y])
        T[Y] = X;
    else T[X] = Y;
 
 
}
 
void Solve() {
 
    for(int i = 1; i <= N; ++i)
        T[i] = i;
 
    sort(E.begin(), E.end());
 
    for(unsigned i = 0; i < E.size(); ++i){
 
        int FindRight = Find(E[i].X);
        int FindLeft = Find(E[i].Y);
 
        if(FindLeft != FindRight) {
 
            Union(FindLeft, FindRight);
            Result.push_back(E[i].Ind);
        }
    }
}
 
 
void Print() {
 
    sort(Result.begin(), Result.end());
 
    for(unsigned i = 0 ;i < Result.size(); ++i)
        fout << Result[i] <<'\n';
}
 
int main() {
 
    Read ();
 
    Solve ();
 
    Print ();
 
    return 0;
}