Pagini recente » Cod sursa (job #3134047) | Cod sursa (job #1525672) | Cod sursa (job #2357168) | Cod sursa (job #418431) | Cod sursa (job #556927)
Cod sursa(job #556927)
#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].nr = i;
}
}
bool sorteaza( muchie a, muchie b )
{
return a.c1 < b.c1 || ( a.c1 == b.c1 && a.c2 > b.c2 );
}
void Solve()
{
sort( G+1, G+m+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]++;
}
}