Pagini recente » Cod sursa (job #2363408) | Cod sursa (job #358871) | Cod sursa (job #2907903) | Cod sursa (job #1741101) | Cod sursa (job #2123616)
#include <fstream>
#include <algorithm>
using namespace std;
pair<int,pair<int,int> > graf[200002];
pair<int,int> sol[400002];
int w[200002];
int tata(int k)
{
while(w[k]>0)
{
k=w[k];
}
return k;
}
int main()
{
int n,m,i,cost,rx,ry,a,b,c,nr;
ifstream in("apm.in");
ofstream out("apm.out");
in>>n>>m;
for(i=1;i<=m;i++)
{
in>>a>>b>>c;
graf[i].first=c;
graf[i].second.first=a;
graf[i].second.second=b;
}
sort(graf+1,graf+m+1);
nr=0;
cost=0;
for(i=1;i<=m;i++)
{
rx=tata(graf[i].second.first);
ry=tata(graf[i].second.second);
if(rx!=ry)
{
nr++;
sol[nr].first=graf[i].second.first;
sol[nr].second=graf[i].second.second;
cost+=graf[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"<<nr<<"\n";
for(i=1;i<=nr;i++)
{
out<<sol[i].first<<" "<<sol[i].second<<"\n";
}
return 0;
}