Pagini recente » Cod sursa (job #3207526) | Cod sursa (job #1370565) | Cod sursa (job #2791263) | Cod sursa (job #1329310) | Cod sursa (job #2152484)
#include <bits/stdc++.h>
using namespace std;
ifstream in("apm.in");
ofstream out("apm.out");
struct muchie
{
int x,y,c;
}a[200005];
int n,m,parent[200005],sz[200005],cost;
bool cmp( const muchie &x, const muchie &y )
{
return( x.c < y.c );
}
int Find( int x )
{
if( parent[x] == x ) return x;
return Find( parent[x] );
}
pair < int , int > sol[200005];
void Union( int x, int y )
{
x = Find(x);
y = Find(y);
if( sz[x] > sz[y] )
{
parent[x] = y;
sz[x] +=sz[y];
}else
{
parent[y] = x;
sz[y]+=sz[x];
}
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
in >> n >> m;
for(int i=1; i<=m; i++)
in >> a[i].x >> a[i].y >> a[i].c;
sort( a+1,a+m+1,cmp);
for(int i =1; i<=n; i++) parent[i] = i, sz[i] = 1;
int i=1,k=0;
while( k < n && i <= m )
{
if( Find(a[i].x) != Find(a[i].y))
{
sol[++k].first = a[i].x;
sol[k].second = a[i].y;
cost+= a[i].c;
Union(a[i].x,a[i].y);
}
i++;
}
out << cost << '\n' << n-1 << '\n';
for(int i=1; i<n; i++)
{
out << sol[i].first << ' ' << sol[i].second << '\n';
}
return 0;
}