Pagini recente » Cod sursa (job #1325440) | Cod sursa (job #21495) | Cod sursa (job #2423976) | Cod sursa (job #1729748) | Cod sursa (job #3214044)
#include <bits/stdc++.h>
using namespace std;
const int Nmax=200005;
ifstream fin("apm.in");
ofstream fout("apm.out");
int n,m,p,i,j;
int father[Nmax],sol;
vector<pair<int,int>> vsol;
struct s
{
int c,x,y;
}v[Nmax];
bool mycmp(s x, s y)
{
return x.c < y.c;
//ce true => switch
}
int findroot(int x)
{
if(father[x] < 0)
return x;
else
father[x] = findroot(father[x]);
return father[x];
}
void unire(int x,int y)
{
int r_x = findroot(x);
int r_y = findroot(y);
if(r_x == r_y)
return;
if( father[r_x] > father[r_y])
swap(r_x, r_y);
father[r_x]+=father[r_y];
father[r_y] = r_x;
}
int main()
{
fin>>n>>m;;
for(i=1; i<=m; i++)
{
fin>>v[i].x>>v[i].y>>v[i].c;
}
sort(v+1, v+m+1,mycmp);
for(i=1; i<=n; i++)
{
father[i] = -1;
}
for(i=1; i<=m; i++)
{
int r_x = findroot(v[i].x);
int r_y = findroot(v[i].y);
if(r_x == r_y)
continue;
else
{
sol+=v[i].c;
unire(v[i].x, v[i].y);
vsol.push_back({v[i].x, v[i].y});
}
}
fout<<sol<<'\n'<<vsol.size()<<'\n';
for(auto i: vsol)
{
fout<<i.first<<' '<<i.second<<'\n';
}
return 0;
}