Pagini recente » Cod sursa (job #2472520) | Cod sursa (job #282213) | Cod sursa (job #2350768) | Cod sursa (job #3136790) | Cod sursa (job #2331665)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
pair<int,pair<int,int>> g[200002];
pair<int,int>sol[400004];
int w[200002];
int tata(int nod)
{
while(w[nod]>0)
{
nod=w[nod];
}
return nod;
}
int main()
{
int n,m,nr,rx,ry,a,b,c,cost;
ifstream in("apm.in");
ofstream out("apm.out");
in>>n>>m;
for(int i=1;i<=m;i++)
{
in>>a>>b>>c;
g[i].first=c;
g[i].second.first=a;
g[i].second.second=b;
}
sort(g+1,g+m+1);
for(int i=1;i<=n;i++)
w[i]=-1;
nr=0;
cost=0;
for(int i=1;i<=m;i++)
{
rx=tata(g[i].second.first);
ry=tata(g[i].second.second);
if(rx!=ry)
{
nr++;
sol[nr].first=g[i].second.first;
sol[nr].second=g[i].second.second;
cost+=g[i].first;
if(w[rx]<w[ry])
{
w[rx]+=w[ry];
w[ry]=rx;
}
else
{
w[ry]+=w[rx];
w[rx]=ry;
}
}
}
out<<cost<<"\n";
out<<nr<<"\n";
for(int i=1;i<=nr;i++)
{
out<<sol[i].first<<" "<<sol[i].second<<"\n";
}
return 0;
}