Cod sursa(job #783375)

Utilizator ionut_blesneagIonut Blesneag ionut_blesneag Data 2 septembrie 2012 17:26:00
Problema Lazy Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.37 kb
#include<cstdio>
#include<algorithm>
using namespace std;

int N,M,i,j,nr,gx,gy;
int x[200002],y[200002],ind[200002],group[200002];
long long c1[200002],c2[200002];
char buff[50],nrc;

struct comp
{bool operator()(const int &a, const int &b)const
  {if(c1[a]==c1[b])
    return(c2[a]>c2[b]);
   else
    return(c1[a]<c1[b]);}
};

inline int group_name(int s)
{if(group[s]==s)
   return s;
 group[s]=group_name(group[s]);
 return group[s];  
}

int unite_groups(int a, int b)
{group[group_name(b)]=group_name(a);}

int get_int()
{int nrn=0;
while(buff[nrc]>='0' && buff[nrc]<='9')
  {nrn=10*nrn+buff[nrc]-'0';
   nrc++;}
return nrn;
}

long long get_long()
{long long nrn=0;
while(buff[nrc]>='0' && buff[nrc]<='9')
  {nrn=10*nrn+buff[nrc]-'0';
   nrc++;}
return nrn;
}

int main()
{freopen("lazy.in","r",stdin);
freopen("lazy.out","w",stdout);
gets(buff);   nrc=0;
N=get_int();  nrc++;
M=get_int();
for(i=1; i<=M; i++)
  {gets(buff);   nrc=0;
   x[i]=get_int();  nrc++;
   y[i]=get_int();  nrc++;
   c1[i]=get_long();  nrc++;
   c2[i]=get_long();  
   ind[i]=i;}
for(i=1; i<=N; i++)
  group[i]=i;   
sort(ind+1,ind+M+1,comp());   
for(i=1; i<=M; i++)
if(nr<N-1)
  {gx=x[ind[i]];  gy=y[ind[i]];
   if(group_name(gx)!=group_name(gy))
    {unite_groups(gx,gy);
    nr++;
    printf("%d\n",ind[i]);}   
   }
else
  break;
return 0;}