Pagini recente » Cod sursa (job #2239922) | Cod sursa (job #2234027) | Cod sursa (job #2451403) | Cod sursa (job #1833471) | Cod sursa (job #3005677)
#include <bits/stdc++.h>
#define INF 1000000007
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
int n, m, x, y, c, d[200001], viz[200001], start, t[200001], sol;
vector <pair < int, pair <int, int> > > l;
int get_root(int x)
{
int root=x;
while(t[root]>0)
{
root=t[root];
}
while(x!=root)
{
int aux=t[x];
t[x]=root;
x=aux;
}
return root;
}
int join(int x, int y)
{
int a=get_root(x);
int b=get_root(y);
if(a==b)
return 0;
if(t[a]<=t[b])
{
t[a]+=t[b];
t[a]=b;
}
else
{
t[b]+=t[a];
t[b]=a;
}
return 1;
}
int main()
{
fin>>n>>m;
for(int i=1; i<=m; i++)
{
fin>>x>>y>>c;
l.push_back({c, {x, y}});
}
sort(l.begin(), l.end());
for(int i=1; i<=n; i++)
t[i]=-1;
int total=0;
vector < pair <int, int> > sol;
for(int i=0; i<l.size(); i++)
{
int cost=l[i].first;
x=l[i].second.first;
y=l[i].second.second;
if(join(x, y)==1)
{
total+=cost;
sol.push_back({x, y});
}
}
fout<<total<<"\n"<<n-1<<"\n";
for(int i=0; i<sol.size(); i++)
{
fout<<sol[i].second<<" "<<sol[i].first<<"\n";
}
return 0;
}