Pagini recente » Cod sursa (job #769916) | Rating Luca Stefan Dugaiasu (LucaStefan) | Cod sursa (job #3167450) | Cod sursa (job #360159) | Cod sursa (job #2264057)
#include <cstdio>
#include <cstdlib>
#define MaxN 200005
#define MaxM 400005
#define min(x, y) (x<y?x:y)
#define swap(x, y) {int aux; aux=x; x=y; y=aux;}
using namespace std;
int i, N, M, Heap[MaxM], Vf, WorkHeap[MaxM], V, Sum, j, Fq[MaxN];
bool Find[MaxN];
struct Pair{
int first;
int second;
}P;
#define PSwap(x, y) {Pair aux; aux=x; x=y; y=aux;}
Pair CList[MaxM];
Pair WorKList[MaxM];
Pair Show[MaxN];
void Read_Sort();
void HeapSort();
void Sift(int i);
void Percolate(int i);
int main()
{
freopen("apm.in", "r", stdin);
freopen("apm.out", "w", stdout);
Read_Sort();
Sum=WorkHeap[1];
V=2; Fq[WorKList[1].first]=Fq[WorKList[1].second]=1;
Show[1]=WorKList[1];
for(i=2; i<N; ++i){
for(j=V; j<=M; ++j){
if(Fq[WorKList[j].first]!=Fq[WorKList[j].second]){
Sum+=WorkHeap[j];
Fq[WorKList[j].first]=Fq[WorKList[j].second]=1;
Show[i]=WorKList[j];
break;
}
}
if(!Show[N-1].first)while(Fq[WorKList[V].first]==1 && Fq[WorKList[V].first]==Fq[WorKList[V].second])++V;
}
printf("%d\n%d\n", Sum, N-1);
for(i=1; i<N; ++i)printf("%d %d\n", Show[i].first, Show[i].second);
return 0;
}
void Read_Sort(){
scanf("%d%d", &N, &M);
Vf=M;
for(i=1; i<=M; ++i){
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
CList[i].first=x;
CList[i].second=y;
Heap[i]=z;
Percolate(i);
}
for(i=1; i<M; ++i){
WorkHeap[i]=Heap[1];
WorKList[i]=CList[1];
Heap[1]=Heap[Vf];
CList[1]=CList[Vf];
Vf--;
Sift(1);
}
WorkHeap[M]=Heap[1];
WorKList[M]=CList[1];
return;
}
void Percolate(int i){
bool k=0;
while(i>1 && k==0){
k=1;
if(Heap[i/2]>Heap[i]){
swap(Heap[i], Heap[i/2]);
PSwap(CList[i], CList[i/2]);
i/=2;
k=0;
}
}
return;
}
void Sift(int i){
bool k=0;
while(i*2<=Vf && k==0){
k=1;
if(i*2==Vf && Heap[i]>Heap[i*2]){
swap(Heap[i], Heap[i*2]);
PSwap(CList[i], CList[i*2]);
i*=2;
k=0;
}
else if(Heap[i]>min(Heap[i*2], Heap[i*2+1])){
int a=(Heap[i*2]<Heap[i*2+1]?i*2:i*2+1);
swap(Heap[i], Heap[a]);
PSwap(CList[i], CList[a]);
i=a;
k=0;
}
}
}