Cod sursa(job #1759780)

Utilizator fanache99Constantin-Buliga Stefan fanache99 Data 19 septembrie 2016 20:19:09
Problema Lazy Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <fstream>
#include <queue>
#include <cstring>
#include <algorithm>

using namespace std;

ifstream cin("lazy.in");
ofstream cout("lazy.out");

const int MAXM = 200000;
const int MAXN = 200000;

struct Edge {
    int from, to;
    int index;
    long long cost, profit;

    bool operator < (const Edge &other) const {
        if (cost == other.cost)
            return profit > other.profit;
        return cost < other.cost;
    }
};

Edge edge[1 + MAXM];
vector<int> answer;
int dad[1 + MAXN], h[1 + MAXN];

void Initialize(int n) {
    for (int i = 1; i <= n; i++) {
        dad[i] = i;
        h[i] = 1;
    }
}

int FindDad(int node) {
    if (dad[node] == node)
        return node;
    return dad[node] = FindDad(dad[node]);
}

bool Join(int a, int b) {
    a = FindDad(a);
    b = FindDad(b);
    if (a == b)
        return false;
    if (h[a] < h[b])
        dad[a] = b;
    else {
        dad[b] = a;
        if (h[a] == h[b])
            h[a]++;
    }
}

int main() {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        cin >> edge[i].from >> edge[i].to >> edge[i].cost >> edge[i].profit;
        edge[i].index = i;
    }
    sort(edge + 1, edge + m + 1);
    Initialize(n);
    for (int i = 1; i <= m; i++)
        if (Join(edge[i].from, edge[i].to))
            answer.push_back(edge[i].index);
    for (auto &it : answer)
        cout << it << "\n";
    return 0;
}