Pagini recente » Cod sursa (job #1658216) | Cod sursa (job #376528) | Cod sursa (job #1896617) | Cod sursa (job #1488450) | Cod sursa (job #2858100)
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
ifstream in("apm.in");
ofstream out("apm.out");
const int Mmax=400005;
pair <int,int>P[Mmax];
int k;
int N,M,total,tt[Mmax],RG[Mmax];
struct muchie{
int x,y,cost;}v[Mmax];
bool compare(muchie a,muchie b){
return a.cost<b.cost;
}
void read(){
in>>N>>M;
for(int i=1;i<=M;i++)
in>>v[i].x>>v[i].y>>v[i].cost;
sort(v+1,v+M+1,compare);
for(int i=1;i<=N;i++)
{
tt[i]=i;
RG[i]=1;
}
}
int Find(int nod)
{
while(tt[nod]!=nod)
nod=tt[nod];
return nod;
}
void unire(int x,int y){
if(RG[x]<RG[y])
tt[x]=y;
if(RG[x]>RG[y])
tt[y]=x;
if(RG[x]==RG[y])
{
tt[x]=y;
RG[y]++;
}
}
void rezolvare(){
for(int i=1;i<=M;i++){
int tata_x=Find(v[i].x),tata_y=Find(v[i].y);
if(tata_x!=tata_y)
{
unire(tata_x,tata_y);
P[++k].first =v[i].x;
P[k].second=v[i].y;
total+=v[i].cost;
}
}}
void afisare(){
out<<total<<'\n'<<N-1<<'\n';
for(int i=1;i<=k;i++)
out<<P[i].first<<" "<<P[i].second<<'\n';
}
int main()
{ read();
rezolvare();
afisare();
return 0;
}