Pagini recente » Cod sursa (job #2344380) | Cod sursa (job #845583) | Cod sursa (job #239442) | Cod sursa (job #38621) | Cod sursa (job #1576361)
#include<fstream>
#include<algorithm>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
int n,m,father[200005],rigt[200005],i;
int k,s,d[400005],xx,yy;
struct muchie
{
int x,y,c;
}a[400005];
int cmp(muchie nr1,muchie nr2)
{
return nr1.c<nr2.c;
}
int radacina(int xx)
{
while(xx!=father[xx])
xx=father[xx];
return xx;
}
void adauga(int xx,int yy)
{
if(rigt[xx]>rigt[yy])
father[yy]=xx;
else
father[xx]=yy;
if(rigt[xx]==rigt[yy])
++rigt[yy];
}
int main()
{
f>>n>>m;
for(i=1;i<=m;i++)
f>>a[i].x>>a[i].y>>a[i].c;
s=0;
sort(a+1,a+m+1,cmp);
for(i=1;i<=n;i++)
father[i]=rigt[i]=i;
for(i=1;i<=m;i++)
{
xx=radacina(a[i].x);
yy=radacina(a[i].y);
if(xx!=yy)
{
k++;
d[k]=i;
s+=a[i].c;
adauga(xx,yy);
}
}
g<<s<<"\n"<<k<<"\n";
for(i=1;i<=k;i++)
g<<a[d[i]].x<<" "<<a[d[i]].y<<"\n";
return 0;
}