Pagini recente » Cod sursa (job #1278897) | Cod sursa (job #1924020) | Cod sursa (job #1202642) | Cod sursa (job #1692117) | Cod sursa (job #2486220)
#include<bits/stdc++.h>
using namespace std;
ifstream in("apm.in");
ofstream out("apm.out");
#define nrmax 200005
struct muchie{
int x;
int y;
int cst;
};
muchie Edge[nrmax];
vector<int>G[nrmax];
int n,m,cost;
int TT[nrmax],indx[nrmax],cnt,solutie,p[nrmax];
bool comparare(muchie a,muchie b)
{
return (a.cst<b.cst);
}
void read()
{
in>>n>>m;
int k,l;
for(int i=1;i<=m;++i)
{
in>>Edge[i].x
>>Edge[i].y
>>Edge[i].cst;
}
sort(Edge+1,Edge+m+1,comparare);
}
int r(int x)
{
while(TT[x]!=x)
x=TT[x];
return x;
}
void unire(int x,int y)
{
if(p[x]<p[y])
TT[x]=y;
if(p[x]>p[y])
TT[y]=x;
if(p[x]==p[y])
{
TT[y]=x;
p[x]++;
}
}
void Krsl()
{
for(int i=1;i<=n;++i)
TT[i]=i;
for(int i=1;i<=m;i++)
{
int a=r(Edge[i].x);
int b=r(Edge[i].y);
if(a!=b)
{
unire(a,b);
solutie+=Edge[i].cst;
indx[++cnt]=i;
}
}
}
int main(){
read();
Krsl();
out<<solutie<<'\n'<<n-1<<'\n';
for(int i=1;i<=cnt;++i)
out<<Edge[indx[i]].y<<" "<<Edge[indx[i]].x<<'\n';
}