Cod sursa(job #2937681)

Utilizator Alex_tz307Lorintz Alexandru Alex_tz307 Data 10 noiembrie 2022 20:10:23
Problema Lazy Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <bits/stdc++.h>

using namespace std;

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

struct edge_t {
  int u, v, index;
  int64_t c1, c2;

  void read() {
    fin >> u >> v >> c1 >> c2;
  }

  bool operator < (const edge_t &rhs) const {
    if (c1 != rhs.c1) {
      return c1 < rhs.c1;
    }
    return c2 > rhs.c2;
  };
};

struct DSU {
  vector<int> p, sz;

  DSU(int n) {
    p.resize(n + 1);
    iota(p.begin(), p.end(), 0);
    sz.assign(n + 1, 1);
  }

  int root(int x) {
    if (x == p[x]) {
      return x;
    }
    return p[x] = root(p[x]);
  }

  bool unite(int u, int v) {
    int x = root(u), y = root(v);
    if (x == y) {
      return false;
    }
    if (sz[y] < sz[x]) {
      swap(x, y);
    }
    p[x] = y;
    sz[y] += sz[x];
    return true;
  }
};

int main() {
  int n, m;
  fin >> n >> m;
  vector<edge_t> edges(m);
  for (int i = 0; i < m; ++i) {
    edges[i].read();
    edges[i].index = i + 1;
  }
  DSU dsu(n);
  sort(edges.begin(), edges.end());
  for (auto e : edges) {
    if (dsu.unite(e.u, e.v)) {
      fout << e.index << '\n';
    }
  }
  fin.close();
  fout.close();
  return 0;
}