Cod sursa(job #2651211)

Utilizator LeVladzCiuperceanu Vlad LeVladz Data 21 septembrie 2020 19:15:36
Problema Lazy Scor 30
Compilator cpp-64 Status done
Runda apmtraining Marime 1.19 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("lazy.in");
ofstream fout("lazy.out");

int n,m,i,tata[200005],sol[200005];
struct muchie{
    int x;
    int y;
    int c1;
    int c2;
    int poz;
} v[200005];

bool cmp(muchie a, muchie b)
{
    if (a.c1 != b.c1)
        return a.c1 < b.c1;
    else
        return a.c1*a.c2 > b.c1*b.c2;
}

int rad(int x)
{
    while (tata[x] > 0)
        x = tata[x];
    return x;
}

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].poz = i;
    }
    sort(v+1, v+m+1, cmp);
    for (i=1; i<=n; i++)
        tata[i] = -1;
    int k = 0;
    for (i=1; i<=m; i++)
    {
        int rx = rad(v[i].x); int ry = rad(v[i].y);
        if (rx != ry)
        {
            sol[++k] = v[i].poz;
            if (tata[rx] < tata[ry])
            {
                tata[rx] += tata[ry];
                tata[ry] = rx;
            }
            else
            {
                tata[ry] += tata[rx];
                tata[rx] = ry;
            }
        }
    }
    for (i=1; i<=k; i++)
        fout << sol[i] << "\n";
    return 0;
}