Cod sursa(job #556901)

Utilizator david_raucaRauca Ioan David david_rauca Data 16 martie 2011 12:58:08
Problema Lazy Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.63 kb
#include<fstream>
using namespace std;

ifstream fin("lazy.in");
ofstream fout("lazy.out");

struct muchie{
       int x, y, nr;
       long long c1, c2;
};

muchie G[200001];

int t[200001];
int h[200001];

int n, m;

void Read();
void Solve();
int Find( int x );
void Union( int x, int y );

int main()
{
    Read();
    Solve();
    
    fin.close();
    fout.close();
    
    return 0;
}

void Read()
{
     fin >> n >> m;
     t[n] = n;
     for( int i = 1; i <= m; ++i )
     {
          if( i < n )
              t[i] = i;
          fin >> G[i].x >> G[i].y >> G[i].c1 >> G[i].c2;
          //G[i].c2 *= G[i].c1;
          G[i].nr = i;
     }
}

bool sorteaza( muchie a, muchie b )
{
     if( a.c1 < b.c1 ) return 1;
     if( a.c1 == b.c1 && a.c2 > b.c2 ) return 1;
     return 0;
}

void Solve()
{
     sort( G+1, G+n+1, sorteaza );
     int k = 0;
     //for( int i = 1; i <= m; ++i )
          //fout << G[i].x << ' ' << G[i].y << ' ' << G[i].c1 << ' ' << G[i].c2 << ' ' << G[i].nr << '\n';
          
     for( int i = 1; i <= m; ++i )
     {
          int v1 = Find( G[i].x );
          int v2 = Find( G[i].y );
          if( v1 != v2 )
          {
              k++;
              fout << G[i].nr <<'\n';
              Union( v1, v2 );
          }
          if( k == n-1 )
              return;
          }
}

int Find( int x )
{
    if( t[x] != x )
        t[x] = Find( t[x] );
    return t[x];
}

void Union( int x, int y )
{
     if( h[x] > h[y] )
         t[y] = x;
     else
     {
         t[x] = y;
         if( h[x] == h[y] )
             h[y]++;
     }
}