Pagini recente » Statistici asdasdad (sabinlord) | Istoria paginii runda/concurs_2014/clasament | Cod sursa (job #1480864) | Cod sursa (job #140397) | Cod sursa (job #2487993)
#include <fstream>
#include <algorithm>
#define f in
#define g out
using namespace std;
ifstream in ( "apm.in" );
ofstream out( "apm.out" );
struct potae {
int cost, x, y;
} v[400009];
int n, m, i, much, total, rx, ry, t[200009];
pair < int, int > sol[200009];
int rad ( int a ){
while ( t[a] > 0 )
a = t[a];
return a;
}
bool cmp ( potae a, potae b ){
return a.cost < b.cost;
}
int main() {
f>>n>>m;
for ( i=1; i <= m; i++ )
f>>v[i].x>>v[i].y>>v[i].cost;
sort( v+1, v+m+1, cmp );
for ( i=1; i <= n; i++ )
t[i] = -1;
for ( i=1; i <= m && much < n; i++ ){
rx = rad ( v[i].x );
ry = rad ( v[i].y );
if ( rx == ry )
continue;
if ( t[rx] < t[ry] ){
t[rx] += t[ry];
t[ry] = rx;
} else {
t[ry] += t[rx];
t[rx] = ry;
}
much++;
sol[much].first = v[i].x; sol[much].second = v[i].y;
total += v[i].cost;
}
g<<total<<"\n"<<n-1<<"\n";
for ( i=1; i < n; i++ )
g<<sol[i].first<<" "<<sol[i].second<<"\n";
return 0;
}