Pagini recente » Cod sursa (job #2963703) | Cod sursa (job #2371984) | Cod sursa (job #213237) | Cod sursa (job #1914214) | Cod sursa (job #2504273)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream in("apm.in");
ofstream out("apm.out");
struct ura
{
int x,y,cost;
};
ura v[400001];
int tata[200001],sefx,sefy;
int sef(int x)
{
if(x==tata[x])return x;
else return tata[x]=sef(tata[x]);
}
void unire(int x,int y)
{
sefx=sef(x);
sefy=sef(y);
tata[sefx]=sefy;
}
bool cmp(ura x,ura y) {
if (x.cost<y.cost)
return true;
return false;
}
int main()
{
int cnt=0,i,j,n,m,suma=0;
in>>n>>m;
for(i=1;i<=m;i++)
{
in>>v[i].x>>v[i].y>>v[i].cost;
}
sort(v+1,v+1+m,cmp);
for(i=1;i<=n;i++)
tata[i]=i;
for(i=1;i<=m&&cnt!=n-1;i++)
{
if(tata[v[i].x]!=tata[v[i].y])
{unire(v[i].x,v[i].y);
cnt++;
suma=suma+v[i].cost;
v[i].cost=1001;}
}
out<<suma<<'\n';
out<<n-1<<'\n';
for(i=1;i<=m;i++)
{
if(v[i].cost==1001)out<<v[i].x<<" "<<v[i].y<<'\n';
}
return 0;
}