1.1 --- a/lemon/pairing_heap.h Thu Jul 09 02:39:47 2009 +0200
1.2 +++ b/lemon/pairing_heap.h Thu Jul 09 04:07:08 2009 +0200
1.3 @@ -1,8 +1,8 @@
1.4 -/* -*- C++ -*-
1.5 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
1.6 *
1.7 - * This file is a part of LEMON, a generic C++ optimization library
1.8 + * This file is a part of LEMON, a generic C++ optimization library.
1.9 *
1.10 - * Copyright (C) 2003-2008
1.11 + * Copyright (C) 2003-2009
1.12 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.13 * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.14 *
1.15 @@ -20,217 +20,223 @@
1.16 #define LEMON_PAIRING_HEAP_H
1.17
1.18 ///\file
1.19 -///\ingroup auxdat
1.20 -///\brief Pairing Heap implementation.
1.21 +///\ingroup heaps
1.22 +///\brief Pairing heap implementation.
1.23
1.24 #include <vector>
1.25 +#include <utility>
1.26 #include <functional>
1.27 #include <lemon/math.h>
1.28
1.29 namespace lemon {
1.30
1.31 - /// \ingroup auxdat
1.32 + /// \ingroup heaps
1.33 ///
1.34 ///\brief Pairing Heap.
1.35 ///
1.36 - ///This class implements the \e Pairing \e heap data structure. A \e heap
1.37 - ///is a data structure for storing items with specified values called \e
1.38 - ///priorities in such a way that finding the item with minimum priority is
1.39 - ///efficient. \c Compare specifies the ordering of the priorities. In a heap
1.40 - ///one can change the priority of an item, add or erase an item, etc.
1.41 + /// This class implements the \e pairing \e heap data structure.
1.42 + /// It fully conforms to the \ref concepts::Heap "heap concept".
1.43 ///
1.44 - ///The methods \ref increase and \ref erase are not efficient in a Pairing
1.45 - ///heap. In case of many calls to these operations, it is better to use a
1.46 - ///\ref BinHeap "binary heap".
1.47 + /// The methods \ref increase() and \ref erase() are not efficient
1.48 + /// in a pairing heap. In case of many calls of these operations,
1.49 + /// it is better to use other heap structure, e.g. \ref BinHeap
1.50 + /// "binary heap".
1.51 ///
1.52 - ///\param _Prio Type of the priority of the items.
1.53 - ///\param _ItemIntMap A read and writable Item int map, used internally
1.54 - ///to handle the cross references.
1.55 - ///\param _Compare A class for the ordering of the priorities. The
1.56 - ///default is \c std::less<_Prio>.
1.57 - ///
1.58 - ///\sa BinHeap
1.59 - ///\sa Dijkstra
1.60 - ///\author Dorian Batha
1.61 -
1.62 + /// \tparam PR Type of the priorities of the items.
1.63 + /// \tparam IM A read-writable item map with \c int values, used
1.64 + /// internally to handle the cross references.
1.65 + /// \tparam CMP A functor class for comparing the priorities.
1.66 + /// The default is \c std::less<PR>.
1.67 #ifdef DOXYGEN
1.68 - template <typename _Prio,
1.69 - typename _ItemIntMap,
1.70 - typename _Compare>
1.71 + template <typename PR, typename IM, typename CMP>
1.72 #else
1.73 - template <typename _Prio,
1.74 - typename _ItemIntMap,
1.75 - typename _Compare = std::less<_Prio> >
1.76 + template <typename PR, typename IM, typename CMP = std::less<PR> >
1.77 #endif
1.78 class PairingHeap {
1.79 public:
1.80 - typedef _ItemIntMap ItemIntMap;
1.81 - typedef _Prio Prio;
1.82 + /// Type of the item-int map.
1.83 + typedef IM ItemIntMap;
1.84 + /// Type of the priorities.
1.85 + typedef PR Prio;
1.86 + /// Type of the items stored in the heap.
1.87 typedef typename ItemIntMap::Key Item;
1.88 - typedef std::pair<Item,Prio> Pair;
1.89 - typedef _Compare Compare;
1.90 + /// Functor type for comparing the priorities.
1.91 + typedef CMP Compare;
1.92 +
1.93 + /// \brief Type to represent the states of the items.
1.94 + ///
1.95 + /// Each item has a state associated to it. It can be "in heap",
1.96 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
1.97 + /// heap's point of view, but may be useful to the user.
1.98 + ///
1.99 + /// The item-int map must be initialized in such way that it assigns
1.100 + /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
1.101 + enum State {
1.102 + IN_HEAP = 0, ///< = 0.
1.103 + PRE_HEAP = -1, ///< = -1.
1.104 + POST_HEAP = -2 ///< = -2.
1.105 + };
1.106
1.107 private:
1.108 class store;
1.109
1.110 - std::vector<store> container;
1.111 - int minimum;
1.112 - ItemIntMap &iimap;
1.113 - Compare comp;
1.114 - int num_items;
1.115 + std::vector<store> _data;
1.116 + int _min;
1.117 + ItemIntMap &_iim;
1.118 + Compare _comp;
1.119 + int _num_items;
1.120
1.121 public:
1.122 - ///Status of the nodes
1.123 - enum State {
1.124 - ///The node is in the heap
1.125 - IN_HEAP = 0,
1.126 - ///The node has never been in the heap
1.127 - PRE_HEAP = -1,
1.128 - ///The node was in the heap but it got out of it
1.129 - POST_HEAP = -2
1.130 - };
1.131 + /// \brief Constructor.
1.132 + ///
1.133 + /// Constructor.
1.134 + /// \param map A map that assigns \c int values to the items.
1.135 + /// It is used internally to handle the cross references.
1.136 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
1.137 + explicit PairingHeap(ItemIntMap &map)
1.138 + : _min(0), _iim(map), _num_items(0) {}
1.139
1.140 - /// \brief The constructor
1.141 + /// \brief Constructor.
1.142 ///
1.143 - /// \c _iimap should be given to the constructor, since it is
1.144 - /// used internally to handle the cross references.
1.145 - explicit PairingHeap(ItemIntMap &_iimap)
1.146 - : minimum(0), iimap(_iimap), num_items(0) {}
1.147 -
1.148 - /// \brief The constructor
1.149 - ///
1.150 - /// \c _iimap should be given to the constructor, since it is used
1.151 - /// internally to handle the cross references. \c _comp is an
1.152 - /// object for ordering of the priorities.
1.153 - PairingHeap(ItemIntMap &_iimap, const Compare &_comp)
1.154 - : minimum(0), iimap(_iimap), comp(_comp), num_items(0) {}
1.155 + /// Constructor.
1.156 + /// \param map A map that assigns \c int values to the items.
1.157 + /// It is used internally to handle the cross references.
1.158 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
1.159 + /// \param comp The function object used for comparing the priorities.
1.160 + PairingHeap(ItemIntMap &map, const Compare &comp)
1.161 + : _min(0), _iim(map), _comp(comp), _num_items(0) {}
1.162
1.163 /// \brief The number of items stored in the heap.
1.164 ///
1.165 - /// Returns the number of items stored in the heap.
1.166 - int size() const { return num_items; }
1.167 + /// This function returns the number of items stored in the heap.
1.168 + int size() const { return _num_items; }
1.169
1.170 - /// \brief Checks if the heap stores no items.
1.171 + /// \brief Check if the heap is empty.
1.172 ///
1.173 - /// Returns \c true if and only if the heap stores no items.
1.174 - bool empty() const { return num_items==0; }
1.175 + /// This function returns \c true if the heap is empty.
1.176 + bool empty() const { return _num_items==0; }
1.177
1.178 - /// \brief Make empty this heap.
1.179 + /// \brief Make the heap empty.
1.180 ///
1.181 - /// Make empty this heap. It does not change the cross reference
1.182 - /// map. If you want to reuse a heap what is not surely empty you
1.183 - /// should first clear the heap and after that you should set the
1.184 - /// cross reference map for each item to \c PRE_HEAP.
1.185 + /// This functon makes the heap empty.
1.186 + /// It does not change the cross reference map. If you want to reuse
1.187 + /// a heap that is not surely empty, you should first clear it and
1.188 + /// then you should set the cross reference map to \c PRE_HEAP
1.189 + /// for each item.
1.190 void clear() {
1.191 - container.clear();
1.192 - minimum = 0;
1.193 - num_items = 0;
1.194 + _data.clear();
1.195 + _min = 0;
1.196 + _num_items = 0;
1.197 }
1.198
1.199 - /// \brief \c item gets to the heap with priority \c value independently
1.200 - /// if \c item was already there.
1.201 + /// \brief Set the priority of an item or insert it, if it is
1.202 + /// not stored in the heap.
1.203 ///
1.204 - /// This method calls \ref push(\c item, \c value) if \c item is not
1.205 - /// stored in the heap and it calls \ref decrease(\c item, \c value) or
1.206 - /// \ref increase(\c item, \c value) otherwise.
1.207 + /// This method sets the priority of the given item if it is
1.208 + /// already stored in the heap. Otherwise it inserts the given
1.209 + /// item into the heap with the given priority.
1.210 + /// \param item The item.
1.211 + /// \param value The priority.
1.212 void set (const Item& item, const Prio& value) {
1.213 - int i=iimap[item];
1.214 - if ( i>=0 && container[i].in ) {
1.215 - if ( comp(value, container[i].prio) ) decrease(item, value);
1.216 - if ( comp(container[i].prio, value) ) increase(item, value);
1.217 + int i=_iim[item];
1.218 + if ( i>=0 && _data[i].in ) {
1.219 + if ( _comp(value, _data[i].prio) ) decrease(item, value);
1.220 + if ( _comp(_data[i].prio, value) ) increase(item, value);
1.221 } else push(item, value);
1.222 }
1.223
1.224 - /// \brief Adds \c item to the heap with priority \c value.
1.225 + /// \brief Insert an item into the heap with the given priority.
1.226 ///
1.227 - /// Adds \c item to the heap with priority \c value.
1.228 - /// \pre \c item must not be stored in the heap.
1.229 + /// This function inserts the given item into the heap with the
1.230 + /// given priority.
1.231 + /// \param item The item to insert.
1.232 + /// \param value The priority of the item.
1.233 + /// \pre \e item must not be stored in the heap.
1.234 void push (const Item& item, const Prio& value) {
1.235 - int i=iimap[item];
1.236 + int i=_iim[item];
1.237 if( i<0 ) {
1.238 - int s=container.size();
1.239 - iimap.set(item, s);
1.240 + int s=_data.size();
1.241 + _iim.set(item, s);
1.242 store st;
1.243 st.name=item;
1.244 - container.push_back(st);
1.245 + _data.push_back(st);
1.246 i=s;
1.247 } else {
1.248 - container[i].parent=container[i].child=-1;
1.249 - container[i].left_child=false;
1.250 - container[i].degree=0;
1.251 - container[i].in=true;
1.252 + _data[i].parent=_data[i].child=-1;
1.253 + _data[i].left_child=false;
1.254 + _data[i].degree=0;
1.255 + _data[i].in=true;
1.256 }
1.257
1.258 - container[i].prio=value;
1.259 + _data[i].prio=value;
1.260
1.261 - if ( num_items!=0 ) {
1.262 - if ( comp( value, container[minimum].prio) ) {
1.263 - fuse(i,minimum);
1.264 - minimum=i;
1.265 + if ( _num_items!=0 ) {
1.266 + if ( _comp( value, _data[_min].prio) ) {
1.267 + fuse(i,_min);
1.268 + _min=i;
1.269 }
1.270 - else fuse(minimum,i);
1.271 + else fuse(_min,i);
1.272 }
1.273 - else minimum=i;
1.274 + else _min=i;
1.275
1.276 - ++num_items;
1.277 + ++_num_items;
1.278 }
1.279
1.280 - /// \brief Returns the item with minimum priority relative to \c Compare.
1.281 + /// \brief Return the item having minimum priority.
1.282 ///
1.283 - /// This method returns the item with minimum priority relative to \c
1.284 - /// Compare.
1.285 - /// \pre The heap must be nonempty.
1.286 - Item top() const { return container[minimum].name; }
1.287 + /// This function returns the item having minimum priority.
1.288 + /// \pre The heap must be non-empty.
1.289 + Item top() const { return _data[_min].name; }
1.290
1.291 - /// \brief Returns the minimum priority relative to \c Compare.
1.292 + /// \brief The minimum priority.
1.293 ///
1.294 - /// It returns the minimum priority relative to \c Compare.
1.295 - /// \pre The heap must be nonempty.
1.296 - const Prio& prio() const { return container[minimum].prio; }
1.297 + /// This function returns the minimum priority.
1.298 + /// \pre The heap must be non-empty.
1.299 + const Prio& prio() const { return _data[_min].prio; }
1.300
1.301 - /// \brief Returns the priority of \c item.
1.302 + /// \brief The priority of the given item.
1.303 ///
1.304 - /// It returns the priority of \c item.
1.305 - /// \pre \c item must be in the heap.
1.306 + /// This function returns the priority of the given item.
1.307 + /// \param item The item.
1.308 + /// \pre \e item must be in the heap.
1.309 const Prio& operator[](const Item& item) const {
1.310 - return container[iimap[item]].prio;
1.311 + return _data[_iim[item]].prio;
1.312 }
1.313
1.314 - /// \brief Deletes the item with minimum priority relative to \c Compare.
1.315 + /// \brief Remove the item having minimum priority.
1.316 ///
1.317 - /// This method deletes the item with minimum priority relative to \c
1.318 - /// Compare from the heap.
1.319 + /// This function removes the item having minimum priority.
1.320 /// \pre The heap must be non-empty.
1.321 void pop() {
1.322 - int TreeArray[num_items];
1.323 + int TreeArray[_num_items];
1.324 int i=0, num_child=0, child_right = 0;
1.325 - container[minimum].in=false;
1.326 + _data[_min].in=false;
1.327
1.328 - if( -1!=container[minimum].child ) {
1.329 - i=container[minimum].child;
1.330 + if( -1!=_data[_min].child ) {
1.331 + i=_data[_min].child;
1.332 TreeArray[num_child] = i;
1.333 - container[i].parent = -1;
1.334 - container[minimum].child = -1;
1.335 + _data[i].parent = -1;
1.336 + _data[_min].child = -1;
1.337
1.338 ++num_child;
1.339 int ch=-1;
1.340 - while( container[i].child!=-1 ) {
1.341 - ch=container[i].child;
1.342 - if( container[ch].left_child && i==container[ch].parent ) {
1.343 + while( _data[i].child!=-1 ) {
1.344 + ch=_data[i].child;
1.345 + if( _data[ch].left_child && i==_data[ch].parent ) {
1.346 i=ch;
1.347 //break;
1.348 } else {
1.349 - if( container[ch].left_child ) {
1.350 - child_right=container[ch].parent;
1.351 - container[ch].parent = i;
1.352 - --container[i].degree;
1.353 + if( _data[ch].left_child ) {
1.354 + child_right=_data[ch].parent;
1.355 + _data[ch].parent = i;
1.356 + --_data[i].degree;
1.357 }
1.358 else {
1.359 child_right=ch;
1.360 - container[i].child=-1;
1.361 - container[i].degree=0;
1.362 + _data[i].child=-1;
1.363 + _data[i].degree=0;
1.364 }
1.365 - container[child_right].parent = -1;
1.366 + _data[child_right].parent = -1;
1.367 TreeArray[num_child] = child_right;
1.368 i = child_right;
1.369 ++num_child;
1.370 @@ -239,8 +245,8 @@
1.371
1.372 int other;
1.373 for( i=0; i<num_child-1; i+=2 ) {
1.374 - if ( !comp(container[TreeArray[i]].prio,
1.375 - container[TreeArray[i+1]].prio) ) {
1.376 + if ( !_comp(_data[TreeArray[i]].prio,
1.377 + _data[TreeArray[i+1]].prio) ) {
1.378 other=TreeArray[i];
1.379 TreeArray[i]=TreeArray[i+1];
1.380 TreeArray[i+1]=other;
1.381 @@ -250,8 +256,8 @@
1.382
1.383 i = (0==(num_child % 2)) ? num_child-2 : num_child-1;
1.384 while(i>=2) {
1.385 - if ( comp(container[TreeArray[i]].prio,
1.386 - container[TreeArray[i-2]].prio) ) {
1.387 + if ( _comp(_data[TreeArray[i]].prio,
1.388 + _data[TreeArray[i-2]].prio) ) {
1.389 other=TreeArray[i];
1.390 TreeArray[i]=TreeArray[i-2];
1.391 TreeArray[i-2]=other;
1.392 @@ -259,88 +265,91 @@
1.393 fuse( TreeArray[i-2], TreeArray[i] );
1.394 i-=2;
1.395 }
1.396 - minimum = TreeArray[0];
1.397 + _min = TreeArray[0];
1.398 }
1.399
1.400 if ( 0==num_child ) {
1.401 - minimum = container[minimum].child;
1.402 + _min = _data[_min].child;
1.403 }
1.404
1.405 - if (minimum >= 0) container[minimum].left_child = false;
1.406 + if (_min >= 0) _data[_min].left_child = false;
1.407
1.408 - --num_items;
1.409 + --_num_items;
1.410 }
1.411
1.412 - /// \brief Deletes \c item from the heap.
1.413 + /// \brief Remove the given item from the heap.
1.414 ///
1.415 - /// This method deletes \c item from the heap, if \c item was already
1.416 - /// stored in the heap. It is quite inefficient in Pairing heaps.
1.417 + /// This function removes the given item from the heap if it is
1.418 + /// already stored.
1.419 + /// \param item The item to delete.
1.420 + /// \pre \e item must be in the heap.
1.421 void erase (const Item& item) {
1.422 - int i=iimap[item];
1.423 - if ( i>=0 && container[i].in ) {
1.424 - decrease( item, container[minimum].prio-1 );
1.425 + int i=_iim[item];
1.426 + if ( i>=0 && _data[i].in ) {
1.427 + decrease( item, _data[_min].prio-1 );
1.428 pop();
1.429 }
1.430 }
1.431
1.432 - /// \brief Decreases the priority of \c item to \c value.
1.433 + /// \brief Decrease the priority of an item to the given value.
1.434 ///
1.435 - /// This method decreases the priority of \c item to \c value.
1.436 - /// \pre \c item must be stored in the heap with priority at least \c
1.437 - /// value relative to \c Compare.
1.438 + /// This function decreases the priority of an item to the given value.
1.439 + /// \param item The item.
1.440 + /// \param value The priority.
1.441 + /// \pre \e item must be stored in the heap with priority at least \e value.
1.442 void decrease (Item item, const Prio& value) {
1.443 - int i=iimap[item];
1.444 - container[i].prio=value;
1.445 - int p=container[i].parent;
1.446 + int i=_iim[item];
1.447 + _data[i].prio=value;
1.448 + int p=_data[i].parent;
1.449
1.450 - if( container[i].left_child && i!=container[p].child ) {
1.451 - p=container[p].parent;
1.452 + if( _data[i].left_child && i!=_data[p].child ) {
1.453 + p=_data[p].parent;
1.454 }
1.455
1.456 - if ( p!=-1 && comp(value,container[p].prio) ) {
1.457 + if ( p!=-1 && _comp(value,_data[p].prio) ) {
1.458 cut(i,p);
1.459 - if ( comp(container[minimum].prio,value) ) {
1.460 - fuse(minimum,i);
1.461 + if ( _comp(_data[_min].prio,value) ) {
1.462 + fuse(_min,i);
1.463 } else {
1.464 - fuse(i,minimum);
1.465 - minimum=i;
1.466 + fuse(i,_min);
1.467 + _min=i;
1.468 }
1.469 }
1.470 }
1.471
1.472 - /// \brief Increases the priority of \c item to \c value.
1.473 + /// \brief Increase the priority of an item to the given value.
1.474 ///
1.475 - /// This method sets the priority of \c item to \c value. Though
1.476 - /// there is no precondition on the priority of \c item, this
1.477 - /// method should be used only if it is indeed necessary to increase
1.478 - /// (relative to \c Compare) the priority of \c item, because this
1.479 - /// method is inefficient.
1.480 + /// This function increases the priority of an item to the given value.
1.481 + /// \param item The item.
1.482 + /// \param value The priority.
1.483 + /// \pre \e item must be stored in the heap with priority at most \e value.
1.484 void increase (Item item, const Prio& value) {
1.485 erase(item);
1.486 push(item,value);
1.487 }
1.488
1.489 - /// \brief Returns if \c item is in, has already been in, or has never
1.490 - /// been in the heap.
1.491 + /// \brief Return the state of an item.
1.492 ///
1.493 - /// This method returns PRE_HEAP if \c item has never been in the
1.494 - /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
1.495 - /// otherwise. In the latter case it is possible that \c item will
1.496 - /// get back to the heap again.
1.497 + /// This method returns \c PRE_HEAP if the given item has never
1.498 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
1.499 + /// and \c POST_HEAP otherwise.
1.500 + /// In the latter case it is possible that the item will get back
1.501 + /// to the heap again.
1.502 + /// \param item The item.
1.503 State state(const Item &item) const {
1.504 - int i=iimap[item];
1.505 + int i=_iim[item];
1.506 if( i>=0 ) {
1.507 - if( container[i].in ) i=0;
1.508 + if( _data[i].in ) i=0;
1.509 else i=-2;
1.510 }
1.511 return State(i);
1.512 }
1.513
1.514 - /// \brief Sets the state of the \c item in the heap.
1.515 + /// \brief Set the state of an item in the heap.
1.516 ///
1.517 - /// Sets the state of the \c item in the heap. It can be used to
1.518 - /// manually clear the heap when it is important to achive the
1.519 - /// better time complexity.
1.520 + /// This function sets the state of the given item in the heap.
1.521 + /// It can be used to manually clear the heap when it is important
1.522 + /// to achive better time complexity.
1.523 /// \param i The item.
1.524 /// \param st The state. It should not be \c IN_HEAP.
1.525 void state(const Item& i, State st) {
1.526 @@ -348,7 +357,7 @@
1.527 case POST_HEAP:
1.528 case PRE_HEAP:
1.529 if (state(i) == IN_HEAP) erase(i);
1.530 - iimap[i]=st;
1.531 + _iim[i]=st;
1.532 break;
1.533 case IN_HEAP:
1.534 break;
1.535 @@ -359,95 +368,95 @@
1.536
1.537 void cut(int a, int b) {
1.538 int child_a;
1.539 - switch (container[a].degree) {
1.540 + switch (_data[a].degree) {
1.541 case 2:
1.542 - child_a = container[container[a].child].parent;
1.543 - if( container[a].left_child ) {
1.544 - container[child_a].left_child=true;
1.545 - container[b].child=child_a;
1.546 - container[child_a].parent=container[a].parent;
1.547 + child_a = _data[_data[a].child].parent;
1.548 + if( _data[a].left_child ) {
1.549 + _data[child_a].left_child=true;
1.550 + _data[b].child=child_a;
1.551 + _data[child_a].parent=_data[a].parent;
1.552 }
1.553 else {
1.554 - container[child_a].left_child=false;
1.555 - container[child_a].parent=b;
1.556 - if( a!=container[b].child )
1.557 - container[container[b].child].parent=child_a;
1.558 + _data[child_a].left_child=false;
1.559 + _data[child_a].parent=b;
1.560 + if( a!=_data[b].child )
1.561 + _data[_data[b].child].parent=child_a;
1.562 else
1.563 - container[b].child=child_a;
1.564 + _data[b].child=child_a;
1.565 }
1.566 - --container[a].degree;
1.567 - container[container[a].child].parent=a;
1.568 + --_data[a].degree;
1.569 + _data[_data[a].child].parent=a;
1.570 break;
1.571
1.572 case 1:
1.573 - child_a = container[a].child;
1.574 - if( !container[child_a].left_child ) {
1.575 - --container[a].degree;
1.576 - if( container[a].left_child ) {
1.577 - container[child_a].left_child=true;
1.578 - container[child_a].parent=container[a].parent;
1.579 - container[b].child=child_a;
1.580 + child_a = _data[a].child;
1.581 + if( !_data[child_a].left_child ) {
1.582 + --_data[a].degree;
1.583 + if( _data[a].left_child ) {
1.584 + _data[child_a].left_child=true;
1.585 + _data[child_a].parent=_data[a].parent;
1.586 + _data[b].child=child_a;
1.587 }
1.588 else {
1.589 - container[child_a].left_child=false;
1.590 - container[child_a].parent=b;
1.591 - if( a!=container[b].child )
1.592 - container[container[b].child].parent=child_a;
1.593 + _data[child_a].left_child=false;
1.594 + _data[child_a].parent=b;
1.595 + if( a!=_data[b].child )
1.596 + _data[_data[b].child].parent=child_a;
1.597 else
1.598 - container[b].child=child_a;
1.599 + _data[b].child=child_a;
1.600 }
1.601 - container[a].child=-1;
1.602 + _data[a].child=-1;
1.603 }
1.604 else {
1.605 - --container[b].degree;
1.606 - if( container[a].left_child ) {
1.607 - container[b].child =
1.608 - (1==container[b].degree) ? container[a].parent : -1;
1.609 + --_data[b].degree;
1.610 + if( _data[a].left_child ) {
1.611 + _data[b].child =
1.612 + (1==_data[b].degree) ? _data[a].parent : -1;
1.613 } else {
1.614 - if (1==container[b].degree)
1.615 - container[container[b].child].parent=b;
1.616 + if (1==_data[b].degree)
1.617 + _data[_data[b].child].parent=b;
1.618 else
1.619 - container[b].child=-1;
1.620 + _data[b].child=-1;
1.621 }
1.622 }
1.623 break;
1.624
1.625 case 0:
1.626 - --container[b].degree;
1.627 - if( container[a].left_child ) {
1.628 - container[b].child =
1.629 - (0!=container[b].degree) ? container[a].parent : -1;
1.630 + --_data[b].degree;
1.631 + if( _data[a].left_child ) {
1.632 + _data[b].child =
1.633 + (0!=_data[b].degree) ? _data[a].parent : -1;
1.634 } else {
1.635 - if( 0!=container[b].degree )
1.636 - container[container[b].child].parent=b;
1.637 + if( 0!=_data[b].degree )
1.638 + _data[_data[b].child].parent=b;
1.639 else
1.640 - container[b].child=-1;
1.641 + _data[b].child=-1;
1.642 }
1.643 break;
1.644 }
1.645 - container[a].parent=-1;
1.646 - container[a].left_child=false;
1.647 + _data[a].parent=-1;
1.648 + _data[a].left_child=false;
1.649 }
1.650
1.651 void fuse(int a, int b) {
1.652 - int child_a = container[a].child;
1.653 - int child_b = container[b].child;
1.654 - container[a].child=b;
1.655 - container[b].parent=a;
1.656 - container[b].left_child=true;
1.657 + int child_a = _data[a].child;
1.658 + int child_b = _data[b].child;
1.659 + _data[a].child=b;
1.660 + _data[b].parent=a;
1.661 + _data[b].left_child=true;
1.662
1.663 if( -1!=child_a ) {
1.664 - container[b].child=child_a;
1.665 - container[child_a].parent=b;
1.666 - container[child_a].left_child=false;
1.667 - ++container[b].degree;
1.668 + _data[b].child=child_a;
1.669 + _data[child_a].parent=b;
1.670 + _data[child_a].left_child=false;
1.671 + ++_data[b].degree;
1.672
1.673 if( -1!=child_b ) {
1.674 - container[b].child=child_b;
1.675 - container[child_b].parent=child_a;
1.676 + _data[b].child=child_b;
1.677 + _data[child_b].parent=child_a;
1.678 }
1.679 }
1.680 - else { ++container[a].degree; }
1.681 + else { ++_data[a].degree; }
1.682 }
1.683
1.684 class store {