Cod sursa(job #2972600)

Utilizator pifaDumitru Andrei Denis pifa Data 29 ianuarie 2023 20:00:44
Problema Lazy Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <bits/stdc++.h>
#define int long long
using namespace std;

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

const int N = 4e5 + 5;

int n, m;

struct elem
{
    int x;
    int y;
    int cost1;
    int cost2;
    int ind;
    bool operator<(const elem p) const{
        if(cost1 == p.cost1)
            return cost2 > p.cost2;
        return cost1 < p.cost1;
    }
} v[N + 1];

int sef[N + 1];

int find(int nod)
{
    if(nod == sef[nod])
        return nod;
    return sef[nod] == find(sef[nod]);
}

void merge(int x, int y)
{
    sef[find(y)] = find(x);
}

signed main()
{
    in >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        sef[i] = i;
    }
    for(int i = 1; i <= m; i++)
    {
        in >> v[i].x >> v[i].y >> v[i].cost1 >> v[i].cost2;
        v[i].ind = i;
    }
    sort(v + 1, v + m + 1);
    for(int i = 1; i <= m; i++)
    {
        int a = find(v[i].x), b = find(v[i].y);
        if(a != b)
        {
            merge(a, b);
            out << v[i].ind << '\n';
        }
    }
    return 0;
}