Pagini recente » Cod sursa (job #412356) | Cod sursa (job #1141728) | Cod sursa (job #1875899) | Cod sursa (job #2830662) | Cod sursa (job #2845070)
#include <bits/stdc++.h>
#define Nmax 200005
#define Mmax 400005
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
struct structura{
int first,second,third;
};
int n,m,T[Nmax],R[Nmax],sum;
vector<structura>M;
vector<pair<int,int> > sol;
int root(int k)
{
if(T[k]==k)
return k;
else return T[k] = root(T[k]);
}
void Union(int a,int b)
{
if(R[a] > R[b])
T[b] = a;
else
{
T[a] = b;
if(R[a] == R[b])
R[b]++;
}
}
bool Compare(structura a, structura b)
{
return a.third < b.third;
}
int main()
{
f>>n>>m;
for(int i = 1; i <= m; i++)
{
int x,y,c;
f>>x>>y>>c;
M.push_back({x,y,c});
}
for(int i = 1; i <= n; i++)
T[i]=i,R[i]=1;
sort(M.begin(),M.end(),Compare);
for(vector<structura>::iterator it = M.begin(); it < M.end(); it++)
{
int x = root(it->first);
int y = root(it->second);
if(T[x]!=T[y])
{
Union(x,y);
sol.push_back({it->first,it->second});
sum+=it->third;
}
}
g<<sum<<"\n";
g<<sol.size()<<"\n";
for(vector<pair<int,int > >::iterator it = sol.begin(); it < sol.end(); it++)
g<<it->first<<" "<<it->second<<"\n";
return 0;
}