Pagini recente » Cod sursa (job #3281000) | Cod sursa (job #254354) | Cod sursa (job #2699363) | Cod sursa (job #3156595) | Cod sursa (job #2529046)
#include <bits/stdc++.h>
using namespace std;
const int Nmax = 400005;
int n,m,k=0,total=0,tata[Nmax],rang[Nmax];
pair<int,int> arb[Nmax];
struct Muchie{
unsigned int x,y;
int cost;
}v[Nmax];
bool Compare(Muchie a,Muchie b){
return a.cost < b.cost;
}
void Read(){
ifstream f("apm.in");
f>>n>>m;
for(int i=1;i<=m;i++)
f>>v[i].x>>v[i].y>>v[i].cost;
sort(v+1,v+m+1,Compare);
for(int i=1;i<=n;i++){
tata[i]=i;
rang[i]=1;
}
f.close();
}
int Find(int nod)
{
while(nod != tata[nod])
nod=tata[nod];
return nod;
}
void Unire(int x,int y){
if(rang[x] < rang[y])
tata[x] = y;
if(rang[x] > rang [y])
tata[y] = x;
if(rang[x] == rang[y]){
tata[x] = y;
rang[y]++;
}
}
void Afisare(){
ofstream g("apm.out");
g<<total<<"\n";
g<<k<<"\n";
for(int i=1;i<=k;i++)
g<<arb[i].first<<" "<<arb[i].second<<"\n";
g.close();
}
void Solve(){
for(int i=1;i<m;i++){
int tata_x = Find(v[i].x);
int tata_y = Find(v[i].y);
if(tata_x != tata_y){
Unire(tata_x ,tata_y);
arb[++k].first = v[i].x;
arb[k].second = v[i].y;
total += v[i].cost;
}
}
Afisare();
}
int main()
{
Read();
Solve();
return 0;
}