Cod sursa(job #2571678)

Utilizator sichetpaulSichet Paul sichetpaul Data 5 martie 2020 09:26:34
Problema Lazy Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <bits/stdc++.h>
#define Nmax 200005
#define ll long long
using namespace std;

ifstream f("lazy.in");
ofstream g("lazy.out");

int N, M;
vector<int> G[Nmax];
struct edge{
    int x, y, pos;
    ll c1, c2;
};
edge v[Nmax];
bool cmp(edge a, edge b) {
    if (a.c1 == b.c1) return a.c2 > b.c2;
    return a.c1 < b.c1;
}

int t[Nmax], h[Nmax];
void unite(int x, int y) {
    if (h[x] < h[y]) swap(x, y);
    t[y] = x;
    if (h[x] == h[y]) ++h[x];
}
int root(int x) {
   while (t[x]) x = t[x];
   return x;
}
int main()
{
    f >> N >> M;
    for (int i = 1; i <= M; ++i)
        f >> v[i].x >> v[i].y >> v[i].c1 >> v[i].c2, v[i].pos = i;
    sort(v + 1, v + M + 1, cmp);

    int nr = N - 1;
    for (int i = 1; i <= M && nr; ++i)
    if (root(v[i].x) != root(v[i].y)) {
        unite(root(v[i].x), root(v[i].y));
        g << v[i].pos << '\n';
        --nr;
    }

    return 0;
}