Pagini recente » Cod sursa (job #1165177) | Cod sursa (job #2694127) | Cod sursa (job #92888) | Cod sursa (job #903628) | Cod sursa (job #2290027)
#include <bits/stdc++.h>
using namespace std;
int nodes, edges, res = 0 , rang[200005], t[200005];
vector < pair <int ,int> > sol;
pair <int , pair <int , int> > edge[400005];
inline int get_tata(int nod)
{
while (t[nod] != nod)
nod = t[nod];
return nod;
}
inline void unite(pair <int , pair <int , int> > muchie)
{
int t1 = get_tata(muchie.second.first);
int t2 = get_tata(muchie.second.second);
if (t1 != t2)
{
if (rang[t1] >= rang[t2])
rang[t1] += (rang[t1] == rang[t2]) , t[t2] = t1;
else t[t1] = t2;
res += muchie.first;
sol.push_back(make_pair(muchie.second.first , muchie.second.second));
}
return ;
}
int main()
{
freopen("apm.in", "r", stdin);
freopen("apm.out", "w", stdout);
scanf("%d %d", &nodes, &edges);
for (int i = 1; i<=edges; ++i)
scanf("%d %d %d", &edge[i].second.first, &edge[i].second.second, &edge[i].first);
sort(edge + 1, edge + edges + 1);
for (int i = 1; i<=nodes; ++i)
t[i] = i , rang[i] = 1;
for (int i = 1; i<=edges; ++i)
unite(edge[i]);
printf("%d\n%d\n", res, (int)sol.size());
for (auto &it : sol)
printf("%d %d\n", it.first, it.second);
return 0;
}