Pagini recente » Cod sursa (job #1146931) | Cod sursa (job #1958106) | Cod sursa (job #1472128) | Cod sursa (job #2703578) | Cod sursa (job #1207707)
#include <cstdio>
#include <algorithm>
using namespace std;
#define MAXM 400005
#define MAXN 200005
struct edge
{
int a, b, w;
}e[MAXM];
struct asd
{
int a, b;
}sol[MAXN];
bool comp(edge a, edge b)
{
return a.w < b.w;
}
int tata[MAXN];
int ftata(int nod);
void uneste(int noda, int nodb);
int main()
{
freopen("apm.in","r",stdin);
freopen("apm.out","w",stdout);
int n, m, i, suma=0, u=0, muchii=0;
scanf("%d%d",&n,&m);
for(i=1; i<=n; i++) tata[i]=i;
for(i=1; i<=m; i++)
scanf("%d%d%d",&e[i].a,&e[i].b,&e[i].w);
sort(e+1,e+m+1,comp);
for(i=1; i<=m; i++)
{
if(ftata(e[i].a) != ftata(e[i].b))
{
uneste(e[i].a,e[i].b);
sol[++u].a=e[i].a; sol[u].b=e[i].b;
suma += e[i].w;
muchii++;
}
}
printf("%d\n%d\n",suma,muchii);
for(i=1; i<=u; i++)
printf("%d %d\n",sol[i].b, sol[i].a);
return 0;
}
int ftata(int nod)
{
if(tata[nod] == nod)
return nod;
int t=ftata(tata[nod]);
tata[nod]=t;
return t;
}
void uneste(int noda, int nodb)
{
int tatab;
tatab = ftata(nodb);
tata[tatab]=ftata(noda);
}