Cod sursa(job #2652518)

Utilizator teisanumihai84Mihai Teisanu teisanumihai84 Data 25 septembrie 2020 06:32:54
Problema Lazy Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <fstream>
#include <algorithm>
#define DIM 200001
using namespace std;
ifstream fin ("lazy.in");
ofstream fout ("lazy.out");
struct muchie {
    int x;
    int y;
    long long  c1;
    long long  c2;
    int nr;
};
muchie v[DIM];
int n, m, t[DIM], i, S, dim, sol[DIM];
bool cmp (muchie i, muchie j)
{
    if (i.c1!=j.c1)
        return i.c1<j.c1;
    else
        return i.c2>j.c2;
}
int rad (int nod)
{
    while (t[nod]>0)
        nod=t[nod];
    return nod;
}
int main()
{
    fin>>n>>m;
    for (i=1; i<=m; i++)
    {
        fin>>v[i].x>>v[i].y>>v[i].c1>>v[i].c2;
        v[i].nr=i;
    }
    sort (v+1, v+m+1, cmp);
    for (i=1; i<=n; i++)
        t[i]=-1;
    for (i=1; i<=m; i++)
    {
        int rx=rad(v[i].x);
        int ry=rad(v[i].y);
        if (rx!=ry)
        {
            if (-t[rx]>-t[ry])
            {
                t[rx]+=t[ry];
                t[ry]=rx;
            }
            else
            {
                t[ry]+=t[rx];
                t[rx]=ry;
            }
            sol[++dim]=v[i].nr;
        }
        if (dim==n-1)
            break;
    }
    for (i=1; i<=dim; i++)
        fout<<sol[i]<<"\n";
}