Pagini recente » Rating Andritoiu Stefan Antoche Ioana Nitu Adrian (UPB_Andritoiu_Antoche_Nitu) | Cod sursa (job #2631881) | Cod sursa (job #691598) | Cod sursa (job #2215947) | Cod sursa (job #3030036)
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
ifstream in("apm.in");
ofstream out("apm.out");
const int NMAX = 2e5 + 5;
const int MMAX = 4e5 + 5;
const char nl = '\n';
struct edge{
int x, y, w;
};
int n, m, sum, father[NMAX];
edge v[MMAX];
vector<edge> tree;
bool cmp(edge a, edge b){
return a.w < b.w;
}
int root(int x){
if(father[x] == x)
return x;
return father[x] = root(father[x]);
}
void unite(int x, int y){
father[root(x)] = root(y);
}
int main()
{
in >> n >> m;
for(int i = 1; i <= m; ++i)
in >> v[i].x >> v[i].y >> v[i].w;
sort(v + 1, v + m + 1, cmp);
for(int i = 1; i <= n; ++i)
father[i] = i;
for(int i = 1; i <= m; ++i){
if(root(v[i].x) != root(v[i].y)){
unite(v[i].x, v[i].y);
tree.pb({v[i].x, v[i].y, v[i].w});
sum = sum + v[i].w;
}
}
out << sum << nl << tree.size() << nl;
for(auto i: tree)
out << i.x << ' ' << i.y << nl;
return 0;
}