Pagini recente » Cod sursa (job #1463001) | Cod sursa (job #2627285) | Cod sursa (job #1693777)
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
struct muchie
{
int a,b,c;
};
int t[200100],n,m,i,c;
muchie v[400100];
vector <int> r;
int root(int x)
{
while(t[x])
x=t[x];
return x;
}
bool compare(muchie a,muchie b)
{
return a.c<b.c;
}
int main()
{
f>>n>>m;
for(i=0; i<m; ++i)
f>>v[i].a>>v[i].b>>v[i].c;
sort(v,v+m,compare);
for(i=0; i<m; ++i)
{
if(root(v[i].a)!=root(v[i].b))
{
c+=v[i].c;
t[root(v[i].a)]=root(v[i].b);
r.push_back(i);
}
}
g<<c<<'\n'<<r.size()<<'\n';
for(i=0; i<r.size(); ++i)
g<<v[r[i]].a<<' '<<v[r[i]].b<<'\n';
}