Cod sursa(job #556775)

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

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

vector<pair<pair<pair<int, int>, pair<int, int> >, int > > G;
int n, m;

int p[200001];
int h[200001];

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

void Read();
void Solve();

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

void Read()
{
     fin >> n >> m;
     G.resize(m+1);
     int x, y, c1, c2;
     p[n] = n;
     for( int i = 1; i <= m; ++i )
     {
          if( i < n )
              p[i] = i;
          fin >> x >> y >> c1 >> c2;
          G[i] = make_pair( make_pair( make_pair(c1,c2), make_pair(x, y) ), i ) ;
     }
}

void Solve()
{
     sort( G.begin()+1, G.end() );
     /*
     for( int i = 1; i <= m; ++i )
          fout << G[i].first.first.first << ' ' << G[i].first.first.second << ' ' << G[i].first.second.first << ' ' << G[i].first.second.second << ' ' << G[i].second << '\n'; 
     */   
     int nr = 0;
     for( int i = 1; i <= m; ++i )
     {
          int v1 = Find( G[i].first.second.first );
          int v2 = Find( G[i].first.second.second );
          if( v1 != v2 )
          {
              nr++;
              fout << G[i].second << '\n';
              Union( v1, v2 );
          }
          
          if( nr == n-1 )
              return;
     }
}

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

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