Pagini recente » Cod sursa (job #1774057) | Cod sursa (job #3283945) | Cod sursa (job #1774045) | Cod sursa (job #2681343) | Cod sursa (job #2170232)
#include <fstream>
#include <algorithm>
#include <vector>
#include <tuple>
#define Nmax 200009
#define Emax 400009
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
typedef tuple <int, int, int> edge;
int n,m,x,y,c,root[Nmax],cost,usage;
vector <int> sol;
vector <edge> E;
bool compare(edge a, edge b) {
return get<2>(a) < get<2>(b);
}
int getroot(int x) {
while (x!=root[x]) {
x=root[x];
}
return x;
}
void reunion(int x, int y) {
root[getroot(y)]=getroot(x);
}
bool check(int x, int y) {
if (getroot(x)==getroot(y)) return 1;
return 0;
}
void ReadInput() {
f>>n>>m;
for (int i=1; i<=m; ++i) {
f>>x>>y>>c;
E.push_back(edge(x,y,c));
}
}
void Solve() {
sort(E.begin(), E.end(), compare);
for (int i=1; i<=n; ++i) root[i]=i;
int counter=1;
for (auto x: E) {
if (!check( get<0>(x) , get<1>(x) )) {
sol.push_back(counter);
cost += get<2>(x);
++usage;
reunion( get<0>(x) , get<1>(x) );
}
++counter;
}
g<<cost<<'\n'<<usage<<'\n';
for (auto x: sol) g<< get<0>(E[x]) <<' '<< get<1>(E[x]) <<'\n';
}
int main() {
ReadInput();
Solve();
f.close(); g.close();
return 0;
}