Pagini recente » Cod sursa (job #1266493) | Cod sursa (job #1327391) | Cod sursa (job #1453716) | Cod sursa (job #567464) | Cod sursa (job #3268661)
#include <bits/stdc++.h>
using namespace std;
ifstream f ("apm.in");
ofstream g ("apm.out");
const int NMAX = 2e5;
const int MMAX = 4e5;
int tata[NMAX+1];
pair<pair<int, int>, int> muchii[MMAX+1];
int sef(int x){
if(x == tata[x])
return x;
return tata[x] = sef(tata[x]);
}
void uneste(int x, int y){
tata[sef(x)] = sef(y);
}
bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b){
return a.second < b.second or (a.second == b.second and a.first.first < b.first.first);
}
vector<pair<int, int>> rez;
int main()
{
int n, m;
f >> n >> m;
int cnt = 0;
for(int i=1; i<=m; i++){
int a, b, c;
f >> a >> b >> c;
cnt ++;
muchii[cnt] = {{a, b}, c};
}
for(int i=1; i<=n; i++)
tata[i] = i;
cnt = 0;
int sum = 0;
sort(muchii + 1, muchii + 1 + m, cmp);
for(int i=1; i<=m; i++){
int a = muchii[i].first.first;
int b = muchii[i].first.second;
int c = muchii[i].second;
if(sef(a) != sef(b)){
rez.push_back({a, b});
sum += c;
uneste(a, b);
}
}
g << sum << "\n" << rez.size() << "\n";
for(auto i : rez)
g << i.first << ' ' << i.second << "\n";
return 0;
}