Pagini recente » Cod sursa (job #1819736) | Cod sursa (job #3151550) | Cod sursa (job #2160272) | Cod sursa (job #2087577) | Cod sursa (job #2675327)
#include <fstream>
#include <algorithm>
#define limM 400000
#define limN 200000
using namespace std;
ifstream in("apm.in");
ofstream out("apm.out");
struct muchie
{
int x, y, c;
bool operator < (const muchie& other) const
{
return c < other.c;
}
}v[limM+5];
int sef[limN+5], n, m, sol[limN+5];
int sefsuprem(int x)
{
if(sef[x]==x) return x;
sef[x] = sefsuprem(sef[x]);
return sef[x];
}
/*
void unire(int x, int y)
{
int sefx = sefsuprem(x);
int sefy = sefsuprem(y);
sef[sefx]=sefy;
}
*/
int main()
{
in>>n>>m;
for(int i=1; i<=m; i++) in>>v[i].x>>v[i].y>>v[i].c;
sort(v+1, v+m+1);
for(int i=1; i<=n; i++) sef[i]=i;
int cnt = 0, total = 0;
for(int i=1; i<=m && cnt<n-1; i++)
{
int sefx = sefsuprem(v[i].x);
int sefy = sefsuprem(v[i].y);
if(sefx != sefy)
{
sef[sefx]=sefy;
total += v[i].c;
cnt++;
sol[cnt]=i;
}
}
out<<total<<'\n'<<cnt<<'\n';
for(int i=1; i<=cnt; i++)
out<<v[sol[i]].x<<' '<<v[sol[i]].y<<'\n';
return 0;
}