话说这个题应该叫 可并堆(左偏树)吧
用斐波那契堆写了一下,网上的资源太少了,pop
函数是半看别人博客半自己 yy 的,所以感觉这里有问题
实际上也是在这个函数里出现 RE,根据输出来看是那个度数(deg
)会一直增大,直到增大到比 n 还大,就很雾
只放 pop
函数,剩下的放二楼
inline data pop(Fib_heap *heap){
if(!heap->min) return (data){-1,0};
data ret=heap->min->val;
Node *min=heap->min,*child;
while(min->child){
child=min->child;
remove(child);
if(child->right==child) min->child=NULL;
else min->child=child->right;
add(heap->min,child);
child->fa=NULL;
}
remove(min);heap->num--;
if(min->right==min) return heap->min=NULL,ret;
reg Node *x,*y;int deg;
heap->min=min=min->right;//min 此时指当前考虑的那一个堆
Node *begin=min;
do{
x=min;
deg=x->deg;
printf("deg : %d\n",deg);
while(tmp[deg]){
y=tmp[deg];
if(x->val>y->val) std::swap(x,y);
printf("swaped: %d\n",deg);
remove(y);
if(!x->child) x->child=y;
else add(x->child,y);
x->deg++;y->fa=x;
// y->mark=0;
tmp[deg]=NULL;
deg++;
}
puts("leave while");
tmp[deg]=x;min=min->right;
if(x->val<heap->min->val) heap->min=x;
}while(min!=begin);
min=begin;
do{tmp[min->deg]=NULL;}while(min!=begin);
return ret;
}