Cod sursa(job #2932416)

Utilizator tomaionutIDorando tomaionut Data 2 noiembrie 2022 20:21:22
Problema Lazy Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, m, k;
struct vrajeala
{
    int x, y, i;
    long long c, p;
    bool operator < (const vrajeala A) const
    {
        if (c == A.c)
            return p > A.p;
        return c < A.c;
    }
}a[200005];

struct DSU
{
    vector <int> t;

    DSU(int n)
    {
        t.resize(n);
    }

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

    int Find(int x)
    {
        int r = x, y;
        while (t[r])
            r = t[r];

        while (r != x)
        {
            y = t[x];
            t[x] = r;
            x = y;
        }

        return r;
    }
};

int main()
{
    int i, x, y, nrcc;
    fin >> n >> m;
    DSU tree(n + 5);
    for (i = 1; i <= m; i++)
    {
        fin >> a[i].x >> a[i].y >> a[i].c >> a[i].p;
        a[i].i = i;
    }
    nrcc = n;
    sort(a + 1, a + m + 1);
    for (i = 1; i <= m and nrcc > 1; i++)
    {
        x = tree.Find(a[i].x);
        y = tree.Find(a[i].y);
        if (x != y)
        {
            tree.Union(x, y);
            fout << a[i].i << "\n";
        }
    }

    return 0;
}