Cod sursa(job #2651808)

Utilizator AlexPascu007Pascu Ionut Alexandru AlexPascu007 Data 23 septembrie 2020 17:01:26
Problema Lazy Scor 100
Compilator cpp-64 Status done
Runda apmtraining Marime 1.05 kb
#include <bits/stdc++.h>
#define DIM 200010
using namespace std;
ifstream fin("lazy.in");
ofstream fout("lazy.out");
long long n,m,i,k,sol[DIM],t[DIM],x,y,rx,ry,j;
struct muchie{
    long long x,y,c1,c2,idx;
};
muchie v[2*DIM];
bool cmp(muchie &a, muchie &b) {
    if (a.c1!=b.c1)
        return a.c1<b.c1;
    return a.c2>b.c2;
}
long long rad(long long nod) {
    while (t[nod]>0)
        nod=t[nod];
    return nod;
}
int main() {
    fin>>n>>m;
    for (i=1;i<=m;i++) {
        fin>>v[i].x>>v[i].y>>v[i].c1>>v[i].c2;
        v[i].idx=i;
    }
    sort(v+1,v+m+1,cmp);
    for (i=1;i<=n;i++)
        t[i]=-1;
    for (i=1;i<=m;i++) {
        x=v[i].x, y=v[i].y;
        rx=rad(x), ry=rad(y);
        if (rx!=ry) {
            sol[++k]=v[i].idx;
            if (t[rx]<t[ry]) {
                t[rx]+=t[ry];
                t[ry]=rx;
            }
            else {
                t[ry]+=t[rx];
                t[rx]=ry;
            }
        }
    }
    for (i=1;i<=k;i++)
        fout<<sol[i]<<"\n";
    return 0;
}