Pagini recente » Cod sursa (job #2735904) | Cod sursa (job #1460228) | Cod sursa (job #2772009) | Cod sursa (job #2506734) | Cod sursa (job #2504726)
#include<bits/stdc++.h>
#define maxn 400005
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
pair<int,int> p[maxn];
int n,m,total,tt[maxn],k,rg[maxn];
struct Edge
{
int x,y,c;
}V[maxn];
bool compara(Edge a,Edge b)
{
return a.c<b.c;
}
void citire()
{
fin>>n>>m;
for(int i=1;i<=m;i++)
fin>>V[i].x>>V[i].y>>V[i].c;
sort(V+1,V+m+1,compara);
/*for(int i=1;i<=m;i++)
cout<<V[i].x<<" "<<V[i].y<<" "<<V[i].c<<"\n";*/
}
int Find(int nod)
{
while(tt[nod]!=nod)
nod=tt[nod];
return nod;
}
void unite(int x,int y)
{
if(rg[x]<rg[y])
tt[x]=y;
if(rg[y]<rg[x])
tt[y]=x;
if(rg[x]==rg[y])
{
tt[x]=y;
rg[y]++;
}
}
void solve()
{
for(int i=1;i<=m;i++)
{
if(Find(V[i].x) !=Find(V[i].y))
{
unite(Find(V[i].x),Find(V[i].y));
p[++k].first =V[i].x;
p[k].second=V[i].y;
total+=V[i].c;
}
}
}
int main()
{
citire();
for(int i=1;i<=m;i++)
{
tt[i]=i;
rg[i]=1;
}
solve();
fout<<total<<"\n";
fout<<n-1<<"\n";
for(int i=1;i<=k;i++)
fout<<p[i].first<<" "<<p[i].second<<"\n";
return 0;
}