Pagini recente » Cod sursa (job #514264) | Cod sursa (job #2626225) | Cod sursa (job #2560658) | Cod sursa (job #1543465) | Cod sursa (job #3204061)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
int n,m,cnt,sum;
int tata[200005];
struct edge
{
int x;int y;
};
edge s[400005];
struct muchie
{
int x;
int y;
int c;
};
bool cmp(muchie &a,muchie &b)
{
return a.c<b.c;
}
muchie g[400005];
void read()
{
fin>>n>>m;
for(int i=1;i<=m;i++)
fin>>g[i].x>>g[i].y>>g[i].c;
sort(g+1,g+m+1,cmp);
}
int rad(int x)
{
while(tata[x]>0)
x=tata[x];
return x;
}
void unite(int x,int y)
{
int rx=rad(x);
int ry=rad(y);
if(tata[rx]<tata[ry])
{
tata[rx]+=tata[ry];
tata[ry]=rx;
}
else
{
tata[ry]+=tata[rx];
tata[rx]=ry;
}
}
void solve()
{
for(int i=1;i<=n;i++)
tata[i]=-1;
for(int i=1;i<=m;i++)
{
int x=g[i].x;
int y=g[i].y;
int c=g[i].c;
if(rad(x)!=rad(y))
{
sum+=c;
s[++cnt]={x,y};
unite(x,y);
}
}
fout<<sum<<"\n"<<cnt<<"\n";
for(int i=1;i<=cnt;i++)
fout<<s[i].x<<" "<<s[i].y<<"\n";
}
int main()
{
read();
solve();
return 0;
}