Pagini recente » Cod sursa (job #1544850) | Cod sursa (job #73850) | Cod sursa (job #1609347) | Cod sursa (job #299231) | Cod sursa (job #2124746)
#include <bits/stdc++.h>
#define in "apm.in"
#define out "apm.out"
#define Nmax 200003
using namespace std;
ifstream fin(in);
ofstream fout(out);
struct muchie
{
int x;
int y;
int cost;
};
int n,m,dim,costmin;
int t[Nmax];
vector <muchie> v;
void Union(int x, int y)
{
t[x] = y;
}
int Find(int x)
{
int r,y;
r = x;
while(t[r] > 0)
r = t[r];
/// comprimarea drumului
while(x != r)
{
y = t[x];
t[x] = r;
x = y;
}
return r;
}
void Read()
{
muchie M;
int i;
fin >> n >> m;
for (i = 1; i <= m; i++)
{
fin >> M.x >> M.y >> M.cost;
v.push_back(M);
}
}
inline bool cmp(muchie A, muchie B)
{
return A.cost < B.cost;
}
void Solve()
{
int i,x,y,costmin = 0;
stack <pair <int,int> > st;
sort(v.begin(),v.end(),cmp);
for (i = 0; i < m; i++)
{
x = Find(v[i].x);
y = Find(v[i].y);
if (x != y)
{
Union(x,y);
costmin += v[i].cost;
st.push({v[i].x,v[i].y});
}
}
fout << costmin << "\n";
fout << st.size() << "\n";
while(!st.empty())
{
fout << st.top().first << " " << st.top().second << "\n";
st.pop();
}
}
int main()
{
Read();
Solve();
fin.close();
fout.close();
return 0;
}