Pagini recente » Cod sursa (job #636265) | Cod sursa (job #1691632) | Cod sursa (job #825316) | Cod sursa (job #2816384) | Cod sursa (job #2274831)
#include <bits/stdc++.h>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
vector <pair<int ,pair<int ,int > > > a;
vector <int > sol;
int n,m,x,y,c,t[200003],nr;
long long s;
inline int reprezentant(int nod)
{
if(t[nod]!=nod)
{
t[nod]=reprezentant(t[nod]);
}
return t[nod];
}
bool verif(int x,int y)
{
x=reprezentant(x);
y=reprezentant(y);
if(x!=y)return true;
return false;
}
void uneste(int x,int y)
{
x=reprezentant(x);
y=reprezentant(y);
t[x]=y;
}
int main()
{
f>>n>>m;
for(int i=1;i<=m;i++)
{
f>>x>>y>>c;
a.push_back(make_pair(c,make_pair(x,y)));
}
sort(a.begin(),a.end());
for(int i=1;i<=n;i++)
{
t[i]=i;
}
for(int i=0;i<m && nr<=n-1;i++)
{
if(verif(a[i].second.first,a[i].second.second))
{
uneste(a[i].second.first,a[i].second.second);
s=s+a[i].first;
sol.push_back(i);
}
}
g<<s<<'\n'<<sol.size()<<'\n';
for (int i=0;i<sol.size();i++)
{
g<<a[sol[i]].second.first<<" "<<a[sol[i]].second.second<<'\n';
}
return 0;
}