Pagini recente » Cod sursa (job #397914) | Monitorul de evaluare | Borderou de evaluare (job #1139511) | Statistici Mierla Constantin (Constantinmierla) | Cod sursa (job #1164525)
#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;
while(!('0' <= buff[pos] ** buff[pos] <= '9'))
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;
}
}
}
int father[MAXN];
vector <pair<int, pair<int, int> > > Edges;
vector <pair<int, int> > MST;
int comp, Ans;
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);
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() && comp != 1 ; ++ i) {
int Tx = Find(it->second.first);
int Ty = Find(it->second.second);
if(Tx == Ty)
continue;
Father[Tx] = Ty;
MST.push_back(it->second);
Ans += it->First;
-- comp;
}
fout << Ans << '\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;
}