Cod sursa(job #1747021)

Utilizator BlackNestaAndrei Manaila BlackNesta Data 24 august 2016 13:45:20
Problema Lazy Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <bits/stdc++.h>
#define Nmax 200005

using namespace std;

int n, m, t[Nmax], sol[Nmax], top;

void Union(int x, int y)
{
    t[y] = x;
}

int Find(int x)
{
    int rad = x;
    while(t[rad] != 0)
        rad = t[rad];
    while(x != rad)
    {
        int aux = t[x];
        t[x] = rad;
        x = aux;
    }
    return rad;
}

struct drum
{
    int x, y, c1, c2, poz;
};

drum a[Nmax];

inline bool cmp(const drum A, const drum B)
{
    if(A.c1 == B.c1) return A.c2 > B.c2;
    return A.c1 < B.c1;
}

void Citire()
{
    ifstream f("lazy.in");
    f >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        f >> a[i].x >> a[i].y >> a[i].c1 >> a[i].c2;
        a[i].poz = i;
    }
    f.close();
    sort(a + 1, a + m + 1, cmp);
}

void Rezolv()
{
    int i, v, w, nrc;
    nrc = n;
    for(i = 1; i <= m && nrc > 1; i++)
    {
        v = a[i].x;
        w = a[i].y;
        v = Find(v);
        w = Find(w);
        if(v != w)
        {
            Union(v, w);
            nrc--;
            sol[++top] = a[i].poz;
        }
    }
}

void Afisare()
{
    ofstream g("lazy.out");
    sort(sol + 1, sol + top + 1);
    for(int i = 1; i <= top; i++)
        g << sol[i] << "\n";
    g.close();
}

int main()
{
    Citire();
    Rezolv();
    Afisare();
    return 0;
}