Cod sursa(job #2441970)

Utilizator Dobricean_IoanDobricean Ionut Dobricean_Ioan Data 22 iulie 2019 12:24:59
Problema Lazy Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.02 kb

#include <fstream>
#include <algorithm>
#define DIM 200002
using namespace std;
ifstream fin ("lazy.in");
ofstream fout ("lazy.out");
int n,m,i,rx,ry,T[DIM],sol[DIM],k;
struct graf{
    int st,dr,poz;
    long long c1,c2;
}L[DIM];

bool cmp(graf x, graf y){
    if(x.c1==y.c1)
        return x.c2>y.c2;
    return x.c1<y.c1;
}

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

int main(){
    fin>>n>>m;
    for(i=1;i<=n;i++)
        T[i]=-1;
    for(i=1;i<=m;i++){
        fin>>L[i].st>>L[i].dr>>L[i].c1>>L[i].c2;
        L[i].poz=i;
    }
    sort(L+1,L+m+1,cmp);
    for(i=1;i<=m;i++){
        rx=rad(L[i].st);
        ry=rad(L[i].dr);
        if(rx!=ry){
            sol[++k]=L[i].poz;
            if(rx<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;
}