Cod sursa(job #1943787)

Utilizator cristibogdanPatrascu Cristian cristibogdan Data 28 martie 2017 20:11:58
Problema Lazy Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.32 kb
#include <fstream>
#include <algorithm>
#include <string.h>
using namespace std;
ifstream f("lazy.in");
ofstream g("lazy.out");

int a,b,n,m,i,tata[200001],nr,xx,yy,s,viz[200001];
long long c1,c2;
struct du{
    int x,y,poz;
    long long cost1,cost2;}muchii[200001];
    int cmp(du u,du v){
            if(u.cost1<v.cost1)
                return 1;
                else
                    if(u.cost1==v.cost1)
                        if(u.cost2>u.cost2)
                        return 1;
            return 0;
            }
int main()
{
   f>>n>>m;
   for(i=1;i<=m;i++){
        f>>a>>b>>c1>>c2;
        muchii[i].x=a;
        muchii[i].y=b;
        muchii[i].cost1=c1;
        muchii[i].cost2=c2;
        muchii[i].poz=i;


   }
   sort(muchii+1,muchii+m+1,cmp);
memset(tata,-1,sizeof(tata));
for(i=1;i<=m&&nr!=(n-1);i++){
    xx=muchii[i].x;
    yy=muchii[i].y;
    while(tata[xx]>0)
        xx=tata[xx];
    while(tata[yy]>0)
        yy=tata[yy];
    if(xx!=yy){
            viz[i]=1;
        if(tata[xx]<tata[yy]){
            tata[xx]+=tata[yy];
            tata[yy]=xx;}
            else{
                tata[yy]+=tata[xx];
                tata[xx]=yy;
            }

        nr++;

    }
}

for(i=1;i<=m;i++)
    if(viz[i]==1)
        g<<muchii[i].poz<<'\n';
    return 0;
}