Pagini recente » Cod sursa (job #2530518) | Cod sursa (job #2975534) | Cod sursa (job #873861) | Cod sursa (job #1161170) | Cod sursa (job #3203493)
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
#define inf INT_MAX
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
struct muchie
{
int x, y, c;
bool operator < (const muchie &alta) const
{
return c<alta.c;
}
};
int n, m, x, y, c, viz[200009], height[200009], cost, muchii;
vector <muchie> M, sol;
int fnd(int x)
{
if(x!=viz[x])
{
return fnd(viz[x]);
}
return viz[x];
}
void uneste(int a, int b)
{
a=fnd(a), b=fnd(b);
if(height[a]>height[b])
{
viz[b]=a;
}
else viz[a]=b;
if(height[a]==height[b])
{
height[b]++;
}
}
void kruskal()
{
for(int i=1; i<=n; i++)
{
viz[i]=i;
}
for(int i=0; i<M.size(); ++i)
{
if(fnd(M[i].x)!=fnd(M[i].y))
{
uneste(M[i].x, M[i].y);
cost+=M[i].c;
sol.push_back(M[i]);
muchii++;
}
}
}
int main()
{
fin>>n>>m;
for(int i=1; i<=m; ++i)
{
fin>>x>>y>>c;
M.push_back({x, y, c});
M.push_back({y, x, c});
}
sort(M.begin(), M.end());
kruskal();
fout<<cost<<"\n"<<muchii<<"\n";
for(int i=0; i<muchii; i++)
{
fout<<sol[i].x<<" "<<sol[i].y<<"\n";
}
return 0;
}