Cod sursa(job #2529215)

Utilizator Ykm911Ichim Stefan Ykm911 Data 23 ianuarie 2020 09:21:53
Problema Lazy Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <bits/stdc++.h>

using namespace std;

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

struct Muchie
{
    int x, y, ind;
    long long cost, profit;
};


Muchie a[200005];
int n, m, t[200005];

bool Cmp(Muchie A, Muchie B)
{
    if(A.cost == B.cost) return A.profit > B.profit;
    return A.cost < B.cost;
}

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

int Find(int x)
{
    int rad = x, y;
    while(t[rad])
        rad = t[rad];
    while(x != rad)
    {
        y = t[x];
        t[x] = rad;
        x = y;
    }
    return rad;
}

void Kruskal()
{
    for(int i = 1; i <= m; i++)
    {
        int x, y;
        x = Find(a[i].x);
        y = Find(a[i].y);
        if(x != y)
        {
            Union(x, y);
            fout << a[i].ind << "\n";
        }
    }
}

int main()
{
    fin >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        int x;
        fin >> a[i].x >> a[i].y >> a[i].cost;
        fin >> x;
        a[i].profit = a[i].cost * x;
        a[i].ind = i;
    }
    sort(a + 1, a + m + 1, Cmp);
    Kruskal();
    return 0;
}