Cod sursa(job #2588019)

Utilizator StanCatalinStanCatalin StanCatalin Data 24 martie 2020 12:15:05
Problema Lazy Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.33 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

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

struct muchie
{
    int x,y;
    long long int c1,c2;
    int care;
};

const int dim = 200005;

int n,m,parent[dim],rang[dim];
muchie v[dim];

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

int Find(int x)
{
    if (x == parent[x])
    {
        return x;
    }
    parent[x] = Find(parent[x]);
    return parent[x];
}

void Union(int x,int y)
{
    int rx = Find(x);
    int ry = Find(y);
    if (rang[rx] < rang[ry])
    {
        parent[rx] = ry;
    }
    else if (rang[rx] > rang[ry])
    {
        parent[ry] = rx;
    }
    else
    {
        rang[rx]++;
        parent[ry] = rx;
    }
}

int main()
{
    in >> n >> m;
    for (int i=1; i<=m; i++)
    {
        int x,y;
        long long int c1,c2;
        in >> x >> y >> c1 >> c2;
        v[i] = {x,y,c1,c2,i};
    }
    for (int i=1; i<=n; i++) parent[i] = i;
    sort(v+1,v+m+1, cmp);
    for (int i=1; i<=m; i++)
    {
        int rx = v[i].x;
        int ry = v[i].y;
        if (Find(rx) != Find(ry))
        {
            Union(rx,ry);
            out << v[i].care << "\n";
        }
    }
    return 0;
}