Cod sursa(job #2502500)

Utilizator MatteoalexandruMatteo Verzotti Matteoalexandru Data 30 noiembrie 2019 22:41:49
Problema Lazy Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.63 kb
/*
                `-/oo+/-   ``
              .oyhhhhhhyo.`od
             +hhhhyyoooos. h/
            +hhyso++oosy- /s
           .yoooossyyo:``-y`
            ..----.` ``.-/+:.`
                   `````..-::/.
                  `..```.-::///`
                 `-.....--::::/:
                `.......--::////:
               `...`....---:::://:
             `......``..--:::::///:`
            `---.......--:::::////+/`
            ----------::::::/::///++:
            ----:---:::::///////////:`
            .----::::::////////////:-`
            `----::::::::::/::::::::-
             `.-----:::::::::::::::-
               ...----:::::::::/:-`
                 `.---::/+osss+:`
                   ``.:://///-.
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cmath>

using namespace std;

const int N = 2e5;

class APM {
    public:
        int parent[5 + N], szg[5 + N];
        struct ura {
            int from, to, idx;
            long long cost, profit;

            bool operator < (const ura a) const {
                if(cost != a.cost) return cost < a.cost;
                return profit > a.profit;
            }
        };
        ura v[5 + N];

        int Find(int x) {
            int y;
            for(y = x; parent[y] != y; y = parent[y]);

            while(parent[x] != x) {
                int cx = parent[x];
                parent[x] = y;
                x = cx;
            }

            return y;
        }

        void Unite(int x, int y) {
            if(szg[x] > szg[y]) {
                parent[y] = x;
                szg[x] += szg[y];
                szg[y] = szg[x];
            } else {
                parent[x] = y;
                szg[y] += szg[x];
                szg[x] = szg[y];
            }
        }
} apm;

int n, m;

int main() {
    freopen("lazy.in", "r", stdin);
    freopen("lazy.out", "w", stdout);
    scanf("%d%d", &n, &m);

    for(int i = 1; i <= m; i++) {
        scanf("%d%d%lld%lld", &apm.v[i].from, &apm.v[i].to, &apm.v[i].cost, &apm.v[i].profit);
        apm.v[i].idx = i;
    }
    sort(apm.v + 1, apm.v + m + 1);

    for(int i = 1; i <= n; i++) {
        apm.parent[i] = i;
        apm.szg[i] = 1;
    }

    for(int i = 1; i <= m; i++) {
        int x, y;
        x = apm.v[i].from;
        y = apm.v[i].to;

        if(apm.Find(x) != apm.Find(y)){
            printf("%d\n", apm.v[i].idx);
            apm.Unite(apm.Find(x), apm.Find(y));
        }
    }
    return 0;
}