Cod sursa(job #2529228)

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

using namespace std;

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

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


Muchie a[200005];
long long n, m, t[200005], ans[200005], k;

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

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

long long Find(long long x)
{
    long long 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++)
    {
        long long x, y;
        x = Find(a[i].x);
        y = Find(a[i].y);
        if(x != y)
        {
            Union(x, y);
            ans[++k] = a[i].ind;
        }
    }
    for(int i = 1; i <= k; i++)
        fout << ans[i] << "\n";
}

int main()
{
    fin >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        long long 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;
}