Pagini recente » Cod sursa (job #2805938) | Cod sursa (job #1605927) | Cod sursa (job #1785645) | Istoria paginii runda/valicaroom2/clasament | Cod sursa (job #2755546)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
#define inf 1e9
pair <int,pair<int,int> >E[200005];
queue<pair<int,int> >Q;
struct muchie
{
int st,dr,c;
} V[200005];
int n,m,x,y,c,L[200005],nr,k,t,ctot,i,T[200005],rang[200005];
bool sel[200005];
int multime(int x)
{
if(T[x]!=x)
T[x]=multime(T[x]);
return T[x];
}
void reuniune(int x,int y)
{
x=multime(x);
y=multime(y);
if(rang[x]>rang[y])
T[y]=x;
else T[x]=y;
if(rang[x]==rang[y])
rang[x]++;
}
void prim()
{
sort(E+1,E+m+1);
for(int i=1; i<=n; i++)
{
sel[i]=false;
L[i]=i;
}
nr=0;
i=1;
while(nr<n-1)
{
x=E[i].second.first;
y=E[i].second.second;
if(L[x]!=L[y])
{nr++;
Q.push(E[i].second);
ctot+=E[i].first;
k=L[x];
t=L[y];
if(multime(x)==multime(y))L[x]=t;
}
i++;
}
}
int main()
{
f>>n>>m;
for(int i=1; i<=m; i++)
{f>>x>>y>>c;
E[i]={c,{x,y}};}
prim();
g<<ctot<<'\n';
g<<Q.size()<<'\n';
while(!Q.empty())
{g<<Q.front().second<<" "<<Q.front().first<<'\n';
Q.pop();}
return 0;
}