-
Notifications
You must be signed in to change notification settings - Fork 438
/
Fibonacci-Heap.cpp
194 lines (178 loc) · 3.08 KB
/
Fibonacci-Heap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Algorithm: Fibonacci Heap
#include <cstdio>
#include <cstring>
#include <algorithm>
#define NIL 0
#define MAX_HEAP_SIZE 100010
using namespace std;
class FibonacciHeap
{
private:
struct node
{
node *child, *parent, *left, *right;
int key, degree;
bool mark;
node(int _key) : child(NIL), parent(NIL), left(NIL), right(NIL), key(_key), degree(0), mark(false) { }
};
node *min;
size_t heap_size;
void _M_insert_to(node *v, node *u);
void _M_insert_to_root_list(node *u);
void _M_remove_from_root_list(node *u);
void _M_link(node *v, node *u);
void _M_consolidate();
public:
FibonacciHeap();
void insert(int key);
int extract_min();
};
void FibonacciHeap::_M_insert_to(node *v, node *u)
{
v->right = u->right;
v->left = u;
u->right->left = v;
u->right = v;
}
void FibonacciHeap::_M_insert_to_root_list(node *u)
{
_M_insert_to(u, min);
}
void FibonacciHeap::_M_remove_from_root_list(node *u)
{
u->left->right = u->right;
u->right->left = u->left;
}
void FibonacciHeap::_M_link(node *v, node *u)
{
_M_remove_from_root_list(v);
if (u->child == NIL)
{
u->child = v;
v->parent = u;
v->left = v->right = v;
}
else
{
_M_insert_to(v, u->child);
v->parent = u;
}
}
void FibonacciHeap::_M_consolidate()
{
static node *root_list[MAX_HEAP_SIZE];
node *first = min;
node *u = first;
int cnt = 0;
do
{
root_list[cnt++] = u;
u = u->right;
} while (u != first);
node *list[50];
memset(list, 0, sizeof(list));
for (int i = 0; i < cnt; i++)
{
node *x = root_list[i];
while (list[x->degree] != NIL)
{
node *y = list[x->degree];
if (x->key > y->key) swap(x, y);
_M_link(y, x);
list[x->degree++] = NIL;
}
list[x->degree] = x;
}
min = NIL;
for (int i = 0; i < 50; i++)
{
if (list[i] != NIL)
{
if (min == NIL)
{
min = list[i];
list[i]->left = list[i]->right = list[i];
}
else
{
_M_insert_to_root_list(list[i]);
if (list[i]->key < min->key) min = list[i];
}
}
}
}
FibonacciHeap::FibonacciHeap() : min(NIL), heap_size(0) { }
void FibonacciHeap::insert(int key)
{
if (min == NIL)
{
min = new node(key);
min->left = min->right = min;
}
else
{
node *u = new node(key);
_M_insert_to_root_list(u);
if (key < min->key) min = u;
}
heap_size++;
}
int FibonacciHeap::extract_min()
{
int res = min->key;
if (min != NIL)
{
if (heap_size == 1)
{
min = NIL;
}
else
{
if (min->child != NIL)
{
static node *child_list[MAX_HEAP_SIZE];
node *first = min->child;
node *u = first;
int cnt = 0;
do
{
child_list[cnt++] = u;
u = u->right;
} while (u != first);
for (int i = 0; i < cnt; i++)
{
_M_insert_to_root_list(child_list[i]);
}
}
_M_remove_from_root_list(min);
min = min->right;
_M_consolidate();
}
}
heap_size--;
return res;
}
int main()
{
FibonacciHeap H;
char op[10];
int x;
while (true)
{
scanf("%s", op);
if (strcmp(op, "push") == 0)
{
scanf("%d", &x);
H.insert(x);
}
else if (strcmp(op, "pop") == 0)
{
printf("%d\n", H.extract_min());
}
else
{
printf("Command not found.\n");
}
}
return 0;
}