Cod sursa(job #749309)

Utilizator geniucosOncescu Costin geniucos Data 16 mai 2012 17:12:04
Problema Arbore partial de cost minim Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int j,ax,ay,n,m,s,i,nr,b[200002],poz[200002];
struct str
{
	int x,y;
	short c;
};
str a[200002];
bool cmp(str a,str b)
{
	return a.c<b.c;
}
vector < int > h[200002];
vector < int >::iterator it;
int main()
{
freopen("apm.in","r",stdin);
freopen("apm.out","w",stdout);
scanf("%d",&n);
scanf("%d",&m);
for(i=1;i<=m;i++)
	scanf("%d %d %d",&a[i].x,&a[i].y,&a[i].c);
sort(a+1,a+m+1,cmp);
for(i=1;i<=n;i++)
{
	h[i].push_back(i);
	b[i]=i;
}
for(i=1;i<=m;i++)
if(b[a[i].x]!=b[a[i].y])
{
	ax=b[a[i].x];
	ay=b[a[i].y];
	for(it=h[ax].begin();it!=h[ax].end();it++)
	{
		b[*it]=ay;
		h[ay].push_back(*it);
	}
	h[ax].clear();
	nr++;
	poz[nr]=i;
	s=s+a[i].c;
	if(nr==n-1) break;
}
printf("%d\n",s);
printf("%d\n",nr);
for(i=1;i<=nr;i++)
	printf("%d %d\n",a[poz[i]].x,a[poz[i]].y);
return 0;
}