Pagini recente » Cod sursa (job #2188539) | Cod sursa (job #1606503) | Cod sursa (job #2910980) | Cod sursa (job #2971449) | Cod sursa (job #404726)
Cod sursa(job #404726)
#include<stdio.h>
#include<algorithm>
#include<bitset>
#define dim 100100
using namespace std;
struct apm
{
int c,x,y;
}a[dim];
int n,m , r[dim] , t[dim] ,cst_min , cnt ;
bitset < dim > f;
inline int cmp(const apm &a ,const apm &b )
{
return a.c<b.c;
}
inline void read ()
{
scanf("%d %d\n",&n,&m);
for(int i=1 ; i <= m; i ++)
scanf("%d %d %d\n", &a[i].x , &a[i].y , & a[i].c);
sort( a+1, a+m+1 , cmp);
}
inline int find(const int &x)
{
if( t[x] != x )
t[x] = find( t[x] );
return t[x] ;
}
inline void unite(const int &x ,const int &y )
{
if( r[ x ] < r[ y ] )
t [ x ] = y ;
else
t [ y ] = x ;
if( r[x] == r[y] )
++r[x];
}
inline void solve()
{
int nod_x, nod_y ;
for(int i=1 ;i<=n; i++)
t[i] = i;
for( int i=1; i<=m; i++ )
{
nod_x = find ( a[i].x ) ;
nod_y = find ( a[i].y ) ;
if( find( nod_x ) != find( nod_y ) )
{
unite ( nod_x , nod_y );
++ cnt ;
f[i] = true ;
cst_min = cst_min + a[ i ].c;
}
}
printf("%d\n%d\n", cnt , cst_min ) ;
for(int i=1 ;i<=m; i++)
if( f [ i ] == true)
{
printf ("%d %d\n",a [ i ].x , a[ i ].y );
}
}
int main ()
{
freopen("apm.in","r",stdin);
freopen("apm.out","w",stdout);
read();
solve();
return 0;
}