Pagini recente » Cod sursa (job #626127) | Borderou de evaluare (job #3146063) | Cod sursa (job #455141) | Cod sursa (job #455139) | Cod sursa (job #995577)
Cod sursa(job #995577)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
#define Nmax 200005
struct edge
{
int x;
int y;
int64_t c1;
int64_t c2;
int ind;
} v[Nmax];
class comp
{
public:
bool operator() ( const edge &a, const edge &b ) const
{
if ( a.c1 != b.c1 )
return a.c1 < b.c1;
return a.c2 > b.c2;
}
};
int tata[Nmax], rang[Nmax];
int N, M;
void read()
{
ifstream f("lazy.in");
f >> N >> M;
for ( int i = 1; i <= M; ++i )
{
int x, y;
int64_t c1, c2;
f >> x >> y >> c1 >> c2;
v[i] = { x, y, c1, c2, i };
}
f.close();
}
void init()
{
for ( int i = 1; i <= N; ++i )
rang[i] = 1,
tata[i] = i;
}
int FIND( int x )
{
int y, R;
for ( R = x; tata[R] != R; R = tata[R] );
for ( ; tata[x] != x; )
{
y = tata[x];
tata[x] = R;
x = y;
}
return R;
}
void UNION( int x, int y )
{
if ( rang[x] > rang[y] )
tata[y] = x;
else
tata[x] = y;
if ( rang[x] == rang[y] )
rang[y]++;
}
void Kruskal()
{
ofstream g("lazy.out");
int use = 0;
for ( int i = 1; i <= M && N - use > 1; ++i )
{
int x = FIND( v[i].x );
int y = FIND( v[i].y );
if ( x != y )
{
g << v[i].ind << "\n";
use++;
UNION( x, y );
}
}
g.close();
}
int main()
{
read();
init();
sort( v + 1, v + M + 1, comp() );
Kruskal();
return 0;
}