Pagini recente » Cod sursa (job #112198) | Cod sursa (job #3259409) | Cod sursa (job #2442958) | Cod sursa (job #2837236) | Cod sursa (job #2860556)
#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<edge> ans;
int n,m,x,y,c;
int link[lim];
int renk[lim];
int dim[lim];
bool mycmp(edge e1,edge e2)
{
return e1.c<e2.c;
}
int tata(int x)
{
int cpy=x,aux;
while(x!=link[x]) x=link[x];
while(cpy!=link[cpy]) aux=cpy,cpy=link[cpy],link[aux]=x;
return x;
}
void unite(int x,int y)
{
x=tata(x);
y=tata(y);
if(renk[x]<renk[y])
swap(x,y);
dim[x]+=dim[y];
link[y]=x;
if(renk[x]==renk[y])
++renk[x];
}
int cost;
int main()
{
in>>n>>m;
for(int i=1;i<=m;++i)
in>>x>>y>>c,
edges.push_back({x,y,c});
for(int i=1;i<=n;++i)
link[i]=i,
renk[i]=1,
dim[i]=1;
sort(edges.begin(),edges.end(),mycmp);
for(edge e:edges)
if(tata(e.x)!=tata(e.y))
cost+=e.c,
unite(e.x,e.y),
ans.push_back(e);
out<<cost<<'\n';
out<<ans.size()<<'\n';
for(edge e:ans)
out<<e.x<<' '<<e.y<<'\n';
return 0;
}