Pagini recente » Cod sursa (job #3240050) | Cod sursa (job #690481) | Cod sursa (job #3153472) | Cod sursa (job #1124081) | Cod sursa (job #3030339)
#include <fstream>
#include<vector>
#include<queue>
#include<algorithm>
#include <cmath>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
int n,m,t[200001],cost,muchii;
vector<pair<int,pair<int,int>>>G;
vector<pair<int,int>>sol;
int get_root(int x)
{
int root = x;
while (t[root] > 0) {
root = t[root];
}
while (x != root) {
int tata = t[x];
t[x] = root;
x = tata;
}
return root;
}
bool join(int x,int y)
{
int root_x= get_root(x);
int root_y= get_root(y);
if(root_x==root_y)
return false;
if(t[root_x]<=t[root_y])
{
t[root_y]+=t[root_x];
t[root_x]=root_y;
}
else
{
t[root_x]+=t[root_y];
t[root_y]=root_x;
}
return true;
}
int main()
{
f>>n>>m;
for(int i=1;i<=n;++i)
t[i]=-1;
while(m--)
{
int x,y,c;
f>>x>>y>>c;
G.push_back(make_pair(c, make_pair(x,y)));
}
sort(G.begin(),G.end());
for(int i=0;i<G.size();++i) {
int c=G[i].first;
int x=G[i].second.first;
int y=G[i].second.second;
if(join(x,y))
{
cost+=c;
muchii++;
sol.push_back(make_pair(x,y));
}
}
g<<cost<<'\n'<<muchii<<'\n';
for(int i=0;i<sol.size();++i)
g<<sol[i].first<<' '<<sol[i].second<<'\n';
}