Pagini recente » Cod sursa (job #2257782) | Cod sursa (job #701894) | Cod sursa (job #1408026) | Cod sursa (job #224317) | Cod sursa (job #3163177)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
const int nmax=400005;
pair<int,int> P[nmax];
int n,m,total,t[nmax],rg[nmax],k;
struct muchie{
int x,y,c;
}v[nmax];
bool comp(muchie a,muchie b){
return a.c<b.c;
}
void Fin(){
fin>>n>>m;
for(int i=1;i<=m;i++)
fin>>v[i].x>>v[i].y>>v[i].c;
sort(v+1,v+m+1,comp);
for(int i=1;i<=n;i++)
t[i]=i,rg[i]=1;
}
int cauta(int x){
while(t[x]!=x)
x=t[x];
return x;
}
int unire(int x,int y){
if(rg[x]<rg[y])
t[x]=y;
if(rg[y]<rg[x])
t[y]=x;
if(rg[x]==rg[y]){
t[x]=y;
rg[y]++;
}
}
void Solve()
{
for(int i=1;i<=m;i++)
{
//cout << "Incerc " << V[i].x << " cu " << V[i].y << "\n";
if(cauta(v[i].x) != cauta(v[i].y))
{
//cout << "Unesc " << V[i].x << " cu " << V[i].y << "\n\n";
unire(cauta(v[i].x), cauta(v[i].y));
P[++k].first = v[i].x;
P[k].second = v[i].y;
total += v[i].c;
}
}
}
void afisare(){
fout<<total<< '\n';
fout<<n-1<<'\n';
for(int i=1;i<=k;i++)
fout<<P[i].first<< " "<<P[i].second<< '\n';
}
int main()
{
Fin();
Solve();
afisare();
/*for(int i=1;i<=m;i++)
fout<<v[i].x<< " "<<v[i].y<< " "<<v[i].c<< '\n';*/
return 0;
}