Pagini recente » Cod sursa (job #3121849) | Cod sursa (job #1138689) | Cod sursa (job #2940825) | Cod sursa (job #1324721) | Cod sursa (job #2791936)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin ("apm.in");
ofstream fout ("apm.out");
int n, m, tt[200005], rg[200005], s, cnt;
pair <int, int> rez[400005];
struct muchie
{
int x, y, c;
}v[400005];
bool cmp(muchie a, muchie b)
{
return a.c < b.c;
}
int findset(int nod)
{
while (nod!=tt[nod])
{
nod=tt[nod];
}
return nod;
}
void unionset(int x, int y)
{
if (rg[x] < rg[y])
tt[x] = y;
else if (rg[y] < rg[x])
tt[y] = x;
else if (rg[y] == rg[x])
{
tt[y] = x;
rg[x]++;
}
}
void apm()
{
for (int i=1; i<=m; i++)
{
int ttx = findset(v[i].x);
int tty = findset(v[i].y);
if (ttx!=tty)
{
unionset(ttx, tty);
cnt++;
rez[cnt]=make_pair(v[i].x,v[i].y);
s+=v[i].c;
}
}
}
int main()
{
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, cmp);
for (int i=1; i<=n; i++)
{
tt[i]=i;
rg[i]=1;
}
apm();
fout << s << '\n';
fout << n-1 << '\n';
for (int i=1; i<=n-1; i++)
{
fout << rez[i].first << " " << rez[i].second << '\n';
}
return 0;
}