Cod sursa(job #2452941)

Utilizator IonAdrianIon Adrian IonAdrian Data 1 septembrie 2019 20:04:56
Problema Lazy Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const int N = 2e5;

struct Edge {
    int x;
    int y;
    ll c1;
    ll c2;
    int ind;
    bool operator < (const Edge &e) {
        if (c1 != e.c1)
            return c1 < e.c1;
        return c2 > e.c2;
    }
};
int dad[1 + N];

int boss (int x) {
    return (dad[x] == x) ? x : (dad[x] = boss (dad[x]));
}

int main() {
    freopen ("lazy.in", "r", stdin);
    freopen ("lazy.out", "w", stdout);
    ios::sync_with_stdio (false);
    cin.tie (0); cout.tie (0);
    int n, m;
    cin >> n >> m;
    vector <Edge> edges;
    int i = 1;
    while (m--) {
        int x, y; ll c1, c2; cin >> x >> y >> c1 >> c2, edges.push_back ({x, y, c1, c2, i++});
    }
    sort (edges.begin (), edges.end ());
    for (int i = 1; i <= n; i++) dad[i] = i;
    for (Edge e : edges) {
        int dx = boss (e.x), dy = boss (e.y);
        if (dx != dy) dad[dx] = dy, cout << e.ind << "\n";
    }
    return 0;
}