Pagini recente » Clasamentul arhivei Infoarena Monthly | Cod sursa (job #913765) | Cod sursa (job #857834) | Cod sursa (job #2862333) | Cod sursa (job #1917859)
#include <bits/stdc++.h>
using namespace std;
int n,m,top,t[400003],s;
struct elem{
int x,y,c;
}v[400003],aux[400003];
inline bool cmp(elem A, elem B)
{
if(A.c<B.c) return true;
return false;
}
int Find(int x)
{
int c=x,k;
while(t[c]!=0)
c=t[c];
while(t[x]!=0)
{
k=t[x];
t[x]=c;
x=k;
}
return c;
}
void Union(int x, int y)
{
t[x]=y;
}
int main()
{
int i,a,b;
ifstream fin("apm.in");
fin>>n>>m;
for(i=1;i<=m;i++)
{
fin>>v[i].x>>v[i].y>>v[i].c;
}
fin.close();
sort(v+1,v+m+1,cmp);
for(i=1;i<=m;i++)
{
a=Find(v[i].x);
b=Find(v[i].y);
if(a!=b)
{
Union(a,b);
aux[++top]=v[i];
s+=v[i].c;
}
}
ofstream fout("apm.out");
fout<<s<<"\n";
for(i=1;i<=top;i++)
{
fout<<aux[i].x<<" "<<aux[i].y<<"\n";
}
fout.close();
return 0;
}