Cod sursa(job #2337321)

Utilizator ptudortudor P ptudor Data 6 februarie 2019 11:49:55
Problema Lazy Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <bits/stdc++.h>
using namespace std;
ifstream in("lazy.in");
ofstream out("lazy.out");
const int nmax=200005;
struct muchie
{
    int a,b,c1,c2,ind;
    bool operator <(muchie other) const
    {
        if (c1==other.c1)return c2>other.c2;
        else return c1<other.c1;
    }
};
int n,m,r[nmax];
muchie l[nmax];
priority_queue<int> q;
void Union(int y,int x)
{
    if (y>x)swap(y,x);
    r[y]=x;
}
int Find(int x)
{
    int rad=x,y;
    while (r[rad]!=0)
    {
        rad=r[rad];
    }
    while (x!=rad)
    {
        y=r[x];
        r[x]=rad;
        x=y;
    }
    return rad;
}
int main()
{
    in>>n>>m;
    int i,A,B,C1,C2,loc=0,y,x,prof=0,cost=0;
    for (i=1;i<=m;i++)
    {
        in>>A>>B>>C1>>C2;
        loc++;
        l[loc].a=A;
        l[loc].b=B;
        l[loc].c1=C1;
        l[loc].c2=C2;
        l[loc].ind=i;
    }
    sort(l+1,l+1+m);
    for (i=1;i<=m;i++)
    {
        A=l[i].a;
        B=l[i].b;
        y=Find(A);
        x=Find(B);
        if (y!=x)
        {
            Union(y,x);
            cost+=l[i].c1;
            prof+=l[i].c2;
            q.push(-1*l[i].ind);
        }
    }
    while (!q.empty())
    {
        out<<q.top()*-1<<"\n";
        q.pop();
    }
    out.close();
    in.close();
    return 0;
}