Pagini recente » Cod sursa (job #2924501) | Cod sursa (job #469573) | Cod sursa (job #2696442) | Cod sursa (job #2183918) | Cod sursa (job #2755258)
#include <bits/stdc++.h>
using namespace std;
ifstream in("apm.in");
ofstream out("apm.out");
int n,m,p[200005];
vector<pair<int,int>> ans;
struct muchie
{
int x,y,c;
};
muchie v[400005];
bool cmp(muchie a,muchie b)
{
return a.c<b.c;
}
int Find(int x)
{
int r=x;
while(r!=p[r]) r=p[r];
while(p[x]!=x)
{
int aux=p[x];
p[x]=r;
x=aux;
}
return r;
}
void Union(int x,int y)
{
int rx=Find(x);
int ry=Find(y);
p[rx]=ry;
}
int apm()
{
for(int i=1;i<=n;i++) p[i]=i;
int cmin=0,k=0;
for(int i=1;i<=m&&k<n-1;i++)
{
if(Find(v[i].x)!=Find(v[i].y))
{
Union(v[i].x,v[i].y);
cmin+=v[i].c;
k++;
ans.push_back({v[i].x,v[i].y});
}
}
return cmin;
}
int main()
{
in>>n>>m;
for(int i=1;i<=m;i++) in>>v[i].x>>v[i].y>>v[i].c;
sort(v+1,v+m+1,cmp);
out<<apm()<<'\n';
out<<n-1<<'\n';
for(auto it:ans) out<<it.first<<" "<<it.second<<'\n';
return 0;
}