Pagini recente » Cod sursa (job #663471) | Cod sursa (job #1564094) | Cod sursa (job #2375294) | Cod sursa (job #1599382) | Cod sursa (job #1164562)
#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";
ofstream fout(outfile);
const int MAXN = 100005;
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; }
const int lim = (1 << 20);
char buff[lim];
int pos;
inline void get(int &X) {
X = 0;
char sgn = '+';
while(!('0' <= buff[pos] && buff[pos] <= '9')) {
sgn = buff[pos];
if(++ pos == lim) {
fread(buff, 1, lim, stdin);
pos = 0;
}
}
while('0' <= buff[pos] && buff[pos] <= '9') {
X = X * 10 + buff[pos] - '0';
if(++ pos == lim) {
fread(buff, 1, lim, stdin);
pos = 0;
}
}
if(sgn == '-')
X = -X;
}
int Father[MAXN];
vector <pair<int, pair<int, int> > > Edges;
vector <pair<int, int> > MST;
int comp, Ans;
int N, M;
inline int Find(int x) {
if(Father[x] != x)
return Father[x] = Find(Father[x]);
return Father[x];
}
int main() {
freopen(infile, "r", stdin);
get(N);
get(M);
for(int i = 1 ; i <= M ; ++ i) {
int x, y, z;
get(x);
get(y);
get(z);
cerr << x << ' ' << y << ' ' << z << '\n';
Edges.push_back(make_pair(z, make_pair(x, y)));
}
sort(Edges.begin(), Edges.end());
for(int i = 1 ; i <= N ; ++ i)
Father[++ comp] = comp;
for(int i = 0 ; i < Edges.size() ; ++ i) {
int Tx = Find(Edges[i].second.first);
int Ty = Find(Edges[i].second.second);
if(Tx == Ty)
continue;
Father[Tx] = Ty;
MST.push_back(Edges[i].second);
Ans += Edges[i].first;
-- comp;
}
fout << Ans << '\n' << N - 1 << '\n';
for(vector <pair<int, int> > :: iterator it = MST.begin(), fin = MST.end(); it != fin ; ++ it)
fout << it->first << ' ' << it->second << '\n';
fout.close();
return 0;
}