Pagini recente » Cod sursa (job #1341571) | Cod sursa (job #2841313) | Cod sursa (job #472851) | Cod sursa (job #2541288) | Cod sursa (job #2776685)
#include <bits/stdc++.h>
using namespace std;
ifstream in("apm.in");
ofstream out("apm.out");
const int lim=2e5+4;
struct edge
{
int x;
int y;
int c;
};
vector<edge> edges;
vector<pair<int,int> > ans;
bool mycmp(edge a,edge b)
{
return a.c<b.c;
}
int link[lim];
int dim[lim];
int tata(int x)
{
int cpy=x,aux;
while(x!=link[x]) x=link[x];
while(cpy!=link[cpy]) aux=link[cpy],link[cpy]=x,cpy=aux;
return x;
}
void unite(int x,int y)
{
x=tata(x);
y=tata(y);
if(x==y)
return ;
if(dim[x]<dim[y])
swap(x,y);
dim[x]+=dim[y];
link[y]=x;
}
int n,m,x,y,c;
int main()
{
in>>n>>m;
for(int i=1;i<=m;++i)
{
in>>x>>y>>c;
edges.push_back({x,y,c});
}
sort(edges.begin(),edges.end(),mycmp);
for(int i=1;i<=n;++i)
link[i]=i,dim[i]=1;
int cost=0;
for(auto p:edges)
{
if(tata(p.x)==tata(p.y))
continue;
cost+=p.c;
unite(p.x,p.y);
ans.push_back({p.x,p.y});
}
out<<cost<<'\n';
out<<ans.size()<<'\n';
for(auto p:ans)
out<<p.first<<' '<<p.second<<'\n';
return 0;
}