Pagini recente » Arhiva de probleme | Cod sursa (job #2854197) | Cod sursa (job #447750) | Cod sursa (job #902885) | Cod sursa (job #2867170)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream in ("apm.in");
ofstream out ("apm.out");
typedef struct
{
int x,y,c;
} muchie;
muchie v[400001];
int t[200001];
int grad[200001];
bool inapm[400001];
int rad(int x)
{
if(t[x]==0)
return x;
t[x]=rad(t[x]);
return t[x];
}
void reuniune(int x,int y)
{
int rx=rad(x),ry=rad(y);
if(grad[x]<grad[y])
{
grad[ry]+=grad[rx];
t[rx]=ry;
}
else
{
grad[rx]+=grad[ry];
t[ry]=rx;
}
}
int cmp(const void *a,const void *b)
{
muchie *e1=(muchie*)a;
muchie *e2=(muchie*)b;
return (e1->c - e2->c);
}
int main()
{
int n,m;
in>>n>>m;
int t,x,y;
for(int i=1;i<=n;i++)
grad[i]=1;
for(int i=0;i<m;i++)
{
in>>v[i].x>>v[i].y>>v[i].c;
}
qsort(v,m,sizeof(v[0]),cmp);
int i=0,nrm=0,cost=0;
while(i<m&&nrm<n-1)
{
if(rad(v[i].x)!=rad(v[i].y))
{
inapm[i]=1;
cost+=v[i].c;
reuniune(v[i].x,v[i].y);
nrm++;
}
i++;
}
out<<cost<<'\n'<<nrm<<'\n';
for(int i=0;i<m;i++)
{
if(inapm[i])
{
out<<v[i].x<<' '<<v[i].y<<'\n';
}
}
return 0;
}