Cod sursa(job #2856516)

Utilizator amcbnCiobanu Andrei Mihai amcbn Data 23 februarie 2022 22:54:55
Problema Lazy Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.47 kb
/// [A][M][C][B][N] ///
#include <bits/stdc++.h>
using namespace std;
const int mod = 9973;
const int inf = 0x3f3f3f3f;
const char sp = ' ', nl = '\n';
ifstream fin("lazy.in");
ofstream fout("lazy.out");

struct dsu {
    vector<int> rt, rnk;
    dsu() {}
    void build(int n) {
        rt.resize(n + 1), rnk.assign(n + 1, 1);
        iota(rt.begin(), rt.end(), 0);
    }
    int find(int x) {
        return rt[x] == x ? x : (rt[x] = find(rt[x]));
    }
    void merge(int x, int y) {
        x = find(x), y = find(y);
        if (x == y) return;
        if (rnk[x] < rnk[y])
            swap(x, y);
        rnk[x] += rnk[y], rnk[y] = 0;
        rt[y] = x;
    }
    bool connected(int x, int y) {
        return find(x) == find(y);
    }
};

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int n, m;
    fin >> n >> m;
    vector<int> src(m + 1), dst(m + 1), cost(m + 1), k(m + 1);
    auto cmp = [&](int a, int b) {
        return cost[a] == cost[b] ? k[a] <= k[b] : cost[a] >= cost[b];
    };
    priority_queue<int, vector<int>, decltype(cmp)> pq(cmp);
    for (int i = 1; i <= m; ++i) {
        fin >> src[i] >> dst[i] >> cost[i] >> k[i];
        pq.push(i);
    }
    dsu tree;
    tree.build(n);
    for (int i = 1; i < n;) {
        int j = pq.top(); pq.pop();
        if (tree.connected(src[j], dst[j]))
            continue;
        tree.merge(src[j], dst[j]);
        fout << j << nl;
        ++i;
    }
}