Pagini recente » Cod sursa (job #2793570) | Cod sursa (job #2604987) | Cod sursa (job #2335235) | Cod sursa (job #1830485) | Cod sursa (job #2804014)
#include <bits/stdc++.h>
using namespace std;
ifstream in("apm.in");
ofstream out("apm.out");
const int lim=2e5+4;
struct edge
{
int x,y,c;
bool operator <(const edge &other) const
{return c<other.c;};
};
vector<edge> edges;
vector<edge> sol;
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=cpy,cpy=link[cpy],link[aux]=x;
return x;
}
void unite(int x,int y)
{
x=tata(x);
y=tata(y);
if(dim[x]<dim[y])
swap(x,y);
dim[x]+=dim[y];
link[y]=x;
}
int n,m,x,y,c,ans;
int main()
{
in>>n>>m;
for(int i=1;i<=n;++i)
link[i]=i,
dim[i]=1;
for(int i=1;i<=m;++i)
in>>x>>y>>c,
edges.push_back({x,y,c});
sort(edges.begin(),edges.end());
for(edge e:edges)
{
x=e.x,y=e.y,c=e.c;
if(tata(x)!=tata(y))
sol.push_back(e),
unite(x,y),
ans+=c;
}
out<<ans<<'\n';
out<<sol.size()<<'\n';
for(edge e:sol)
out<<e.x<<' '<<e.y<<'\n';
return 0;
}