Cod sursa(job #2651894)

Utilizator mihnea03Ciocioiu Mihnea mihnea03 Data 23 septembrie 2020 19:02:56
Problema Lazy Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1 kb
#include <fstream>
#include <algorithm>
#include <vector>
#define dim 200010
using namespace std;
int t[dim];
int i,n,m,r1,r2;

struct muchie {
    int a,b,c1,c2,index;
}c[dim];

int cmp (muchie a,muchie b) {
    if (a.c1!=b.c1) return a.c1<b.c1;
    else return a.c2<b.c2;
}

int rad (int x) {
    while (t[x]>0) {
        x=t[x];
    }
    return x;
}

int main() {
    ifstream fin("lazy.in");
    ofstream fout("lazy.out");
    fin>>n>>m;
    for (i=1;i<=m;i++) {
        fin>>c[i].a>>c[i].b>>c[i].c1>>c[i].c2;
        c[i].index=i;
    }
    sort (c+1,c+m+1,cmp);
    for (i=1;i<=n;i++) {
        t[i]=-1;
    }
    for (i=1;i<=m;i++) {
        r1=rad(c[i].a);
        r2=rad(c[i].b);
        if (r1!=r2) {
            if (t[r1]>t[r2]) {
                t[r2]+=t[r1];
                t[r1]=r2;
            }
            else {
                t[r1]+=t[r2];
                t[r2]=r1;
            }
            fout<<c[i].index<<"\n";
        }
    }
    return 0;
}