Cod sursa(job #2658623)

Utilizator euyoTukanul euyo Data 14 octombrie 2020 16:39:25
Problema Lazy Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <fstream>
#include <algorithm>

using namespace std;

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

struct edge {
  int a, b;
  long long cost1, cost2;
};

const int MaxM = 200002;
const int MaxN = 200002;

edge e[MaxM];
int nxt[MaxN], ind[MaxM];

int cmp( int a, int b ) {
  if ( e[a].cost1 < e[b].cost1 ) {
    return 1; 
  } else if ( e[a].cost1 > e[b].cost1 ) {
	return 0;
  } else {
	if ( e[a].cost2 < e[b].cost2 ) {
      return 0;
	} else {
	  return 1;
	}
  }
}

int root( int x ) {
  if ( nxt[x] == x ) {
	return x;
  }
  return nxt[x] = root( nxt[x] );
}
void join( int x, int y ) {
  nxt[root( x )] = root( y );
}

int main() {
  int n, m;

  fin >> n >> m;
  for ( int i = 0; i < m; ++i ) {
	fin >> e[i].a >> e[i].b >> e[i].cost1 >> e[i].cost2;
    ind[i] = i;
  }
  for ( int i = 0; i < n; ++i ) {
	nxt[i] = i;
  }
  sort( ind, ind + m, cmp );
  for ( int i = 0; i < m; ++i ) {
	if ( root( e[ind[i]].a ) != root( e[ind[i]].b ) ) {
	  join( e[ind[i]].a, e[ind[i]].b );
	  fout << ind[i] + 1 << "\n";
	}
  }
  fin.close();
  fout.close();
  return 0;
}