Pagini recente » Cod sursa (job #1671350) | Cod sursa (job #391352) | Cod sursa (job #1925807) | Cod sursa (job #1207799) | Cod sursa (job #1607977)
#include <cstdio>
#include <algorithm>
using namespace std;
struct poz
{
int x,y,cost;
}muchii[400001];
struct poz1
{
int x,y;
}sol[400001];
int t[200001],h[200001];
int find_(int x)
{
if (t[x]==x) return x;
t[x]=find_(t[x]);
return t[x];
}
bool cmp(poz a,poz b)
{
if (a.cost<b.cost) return 1;
return 0;
}
int union_(int x,int y)
{
int a=find_(x),b=find_(y);
if (h[a]<h[b]) t[a]=b;
else
{
t[b]=a;
if (h[a]==h[b]) h[a]++;
}
}
int main()
{
freopen("apm.in","r",stdin);
freopen("apm.out","w",stdout);
int n,m;
scanf("%d %d",&n,&m);
for (int i=1;i<=n;i++)
t[i]=i;
int cnt=0;
for (int i=1;i<=m;i++)
scanf("%d %d %d\n",&muchii[i].x,&muchii[i].y,&muchii[i].cost);
sort(muchii+1,muchii+m+1,cmp);
int cost1=0;
for (int i=1;i<=m && cnt<n-1;i++)
{
int a=muchii[i].x,b=muchii[i].y;
int a1=find_(a),b1=find_(b);
if (a1!=b1)
{
cnt++;
union_(a,b);
cost1+=muchii[i].cost;
sol[cnt].x=b;
sol[cnt].y=a;
}
}
printf("%d\n",cost1);
printf("%d\n",n-1);
for (int i=1;i<=n-1;i++)
printf("%d %d\n",sol[i].x,sol[i].y);
}