Cod sursa(job #1553852)

Utilizator elevenstrArina Raileanu elevenstr Data 20 decembrie 2015 17:00:28
Problema Lazy Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <bits/stdc++.h>

using namespace std;
//http://www.infoarena.ro/problema/lazy
#define MAX 200001
long long tt[MAX];
FILE* fila=fopen("lazy.in","r");
ofstream out("lazy.out");
struct muchie
{long long x,y,ind,cost,profi;
}mu[MAX];
bool cmp(muchie a, muchie b)
{
   if(a.cost<b.cost)return 1;
   if(a.cost==b.cost&&a.profi<b.profi)return 1;
   return 0;
}
vector<long long> v;
const int LIM=4000;
char buff[LIM];
int poz=0;
void cit(long long &x)
{  while(!isdigit(buff[poz]))
{
   ++poz;
    if(poz==LIM)
    {
        poz=0;
        fread(buff,1,LIM,fila);
    }
}
   x=0;
   while(isdigit(buff[poz]))
   {
       x=x*10+buff[poz]-'0';
       ++poz;
       if(poz==LIM)
        {
        poz=0;
        fread(buff,1,LIM,fila);
        }
   }
}
long long find_t(long long x)
{
    if(tt[x]==x)
        return x;
    long long t=find_t(tt[x]);
    tt[x]=t;
    return t;
}
int main()
{   long long n,m;
    cit(n);cit(m);
    for(int i=1;i<=m;i++)
    {
        cit(mu[i].x);
        cit(mu[i].y);
        cit(mu[i].cost);
        cit(mu[i].profi);
        mu[i].ind=i;
    }
    for(int i=1;i<=n;i++)
        tt[i]=i;
    sort(mu+1,mu+m+1,cmp);
    for(int i=1;i<=m;i++)
        if(find_t(mu[i].x)!=find_t(mu[i].y))
        {  tt[find_t(mu[i].y)]=find_t(mu[i].x);
          out<<mu[i].ind<<'\n';
        }

    return 0;
}