Pagini recente » Cod sursa (job #2939902) | Cod sursa (job #3276307) | Cod sursa (job #3123355) | Cod sursa (job #2622737) | Cod sursa (job #1701399)
#include <iostream>
#include <fstream>
#include <set>
#include <map>
#include <deque>
using namespace std;
typedef pair<int,int> paer;
struct nod {
nod *parent = 0;
};
nod* top(nod *x) {
nod* t = x;
while (t->parent) {
t = t->parent;
}
if (t != x) {
x->parent = t;
}
return t;
}
int main()
{
ifstream in("apm.in");
ofstream out("apm.out");
int n, m;
in>>n>>m;
set<pair<int, paer>> muchii;
set<pair<int, paer>>::iterator it;
int x, y, c, i;
for (i=0; i<m; i++) {
in>>x>>y>>c;
paer p = paer(x, y);
muchii.insert(pair<int, paer>(c, p));
}
nod* graf[n+1];
for (i=1; i<=n; i++) {
graf[i] = new nod;
}
paer apm[n-1];
i = 0;
int suma = 0;
for (it = muchii.begin(); it != muchii.end(); it++) {
pair<int, paer> p = *it;
x = p.second.first;
y = p.second.second;
nod *t1 = top(graf[x]), *t2 = top(graf[y]);
if (t1 != t2) {
suma += p.first;
t2->parent = t1;
apm[i] = paer(p.second.first, p.second.second);
i++;
}
}
out<<suma<<endl;
out<<n-1<<endl;
for (i=0; i<n-1; i++) {
out<<apm[i].first<<" "<<apm[i].second<<endl;
}
in.close();
out.close();
return 0;
}