Pagini recente » Cod sursa (job #1137324) | Cod sursa (job #1746748) | Cod sursa (job #3249327) | Cod sursa (job #1952607) | Cod sursa (job #1690202)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
ifstream fin ("apm.in");
ofstream fout ("apm.out");
const int nmax = 4e5+5;
vector <pair<int,int> > sol;
int dad[nmax], rang[nmax];
struct muchie {
int x, y, c;
inline bool operator () (const muchie &a, const muchie &b) {
return a.c < b.c;
}
} v[nmax];
int Find(int x) {
while(dad[x]!=x) x=dad[x];
return x;
}
void Union(int x, int y) {
if(rang[x] > rang[y]) dad[y]=x;
else dad[x]=y;
if(rang[x]==rang[y]) rang[y]++;
}
int main() {
ios_base::sync_with_stdio(false);
int n, m, i, cmin=0, rx, ry;
fin >> n >> m;
for(i=1; i<=m; i++)
fin >> v[i].x >> v[i].y >> v[i].c;
sort(v+1, v+m+1, muchie());
for(i=1; i<=n; i++)
dad[i]=i;
for(i=1; i<=m; i++) {
rx=Find(v[i].x);
ry=Find(v[i].y);
if(rx!=ry) {
cmin+=v[i].c;
Union(rx, ry);
sol.push_back({v[i].x, v[i].y});
}
}
fout << cmin << "\n" << n-1 << "\n";
for(auto it : sol)
fout << it.first << " " << it.second << "\n";
fin.close();
fout.close();
return 0;
}