Pagini recente » Cod sursa (job #2950220) | Cod sursa (job #3290957) | Cod sursa (job #1941702) | Cod sursa (job #1750513) | Cod sursa (job #235754)
Cod sursa(job #235754)
using namespace std;
#include <algorithm>
#include <cstdio>
#include <string>
#define dim 8192
char ax[dim];
int pz;
inline void cit(int &x)
{
x=0;
while( (ax[pz]<'0' || ax[pz]>'9') && ax[pz]!='-')
if(++pz == dim) fread(ax,1,dim,stdin),pz=0;
int neg=0;
if(ax[pz] == '-')
{
neg=1;
if(++pz==dim) fread(ax,1,dim,stdin),pz=0;
}
while(ax[pz]>='0' && ax[pz]<='9')
{
x=x*10+ax[pz]-'0';
if(++pz==dim)fread(ax,1,dim,stdin),pz=0;
}
if(neg) x=-x;
}
struct nod{ int x, y,c;};
struct cmp{
bool operator()(const nod &a,const nod &b)const
{
if(a.c < b.c) return 1;
return 0;
}
};
nod a[400001];
int n, m;
int t[200001];
inline int find(int i)
{
if(t[i] != i) t[i]=find(t[i]);
return t[i];
}
inline int uneste(int i, int j)
{
i=find(i);
j=find(j);
if(i == j) return 0;
t[i]=j;
return 1;
}
int main()
{
freopen("apm.in","r",stdin);
freopen("apm.out","w",stdout);
cit(n);cit(m);
int i;
for(i=1;i<=m;++i) cit(a[i].x), cit(a[i].y), cit(a[i].c);
sort(a+1,a+m+1,cmp());
for(i=1;i<=n;++i) t[i]=i;
int nr=0, sol=0;
for(i=1;i<=m;++i)
if(uneste(a[i].x, a[i].y))
{
sol+=a[i].c;
++nr;
a[i].c=0x3f3f3f3f;
}
printf("%d\n", sol);
printf("%d\n", nr);
for(i=1;i<=m;++i)
if(a[i].c == 0x3f3f3f3f) printf("%d %d\n", a[i].x, a[i].y);
return 0;
}