Pagini recente » Cod sursa (job #2835466) | Cod sursa (job #1834617) | Cod sursa (job #1856072) | Cod sursa (job #884585) | Cod sursa (job #1123028)
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <deque>
using namespace std;
const char infile[] = "apm.in";
const char outfile[] = "apm.out";
ifstream fin(infile);
ofstream fout(outfile);
const int MAXN = 200005;
const int oo = 0x3f3f3f3f;
typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;
const inline int min(const int &a, const int &b) { if( a > b ) return b; return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b; return a; }
const inline void Get_min(int &a, const int b) { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b) { if( a < b ) a = b; }
int N, M, Father[MAXN], apmCost;
vector <pair<int, pair<int, int> > > Edges;
vector <pair<int, int> > Ans;
int Find(const int &Node) {
if(Father[Node] != Node)
Father[Node] = Find(Father[Node]);
return Father[Node];
}
int main() {
fin >> N >> M;
for(int i = 1 ; i <= M ; ++ i) {
int x, y, z;
fin >> x >> y >> z;
Edges.push_back(make_pair(z, make_pair(x, y)));
}
sort(Edges.begin(), Edges.end());
for(int i = 1 ; i <= N ; ++ i)
Father[i] = i;
for(int i = 0 ; Ans.size() < N - 1 ; ++ i) {
int z = Edges[i].first;
int x = Edges[i].second.first;
int y = Edges[i].second.second;
int Tx = Find(x);
int Ty = Find(y);
if(Tx == Ty)
continue;
Father[Tx] = Ty;
apmCost += z;
Ans.push_back(make_pair(x, y));
}
fout << apmCost << '\n' << Ans.size() << '\n';
for(int i = 0 ; i < Ans.size() ; ++ i)
fout << Ans[i].first << ' ' << Ans[i].second << '\n';
fin.close();
fout.close();
return 0;
}