



from __future__ import annotations

from sys import maxsize
from typing import Generic, TypeVar

__lowerCamelCase         =			TypeVar('''T''')







def    _snake_case (       __snake_case    )     ->     int:






      '''simple docstring'''







      return (position - 1) // 2







def    _snake_case (       __snake_case    )     ->     int:






      '''simple docstring'''







      return (2 * position) + 1







def    _snake_case (       __snake_case    )     ->     int:






      '''simple docstring'''







      return (2 * position) + 2


class   snake_case_ (Generic[T]  ):




       """simple docstring"""






       def __init__(     self):

             """simple docstring"""





             UpperCAmelCase_  :	Optional[Any]				      =		[]
             UpperCAmelCase_  :	List[str]				      =		{}
             UpperCAmelCase_  :	Dict				      =		0



       def __len__(     self):

             """simple docstring"""





             return self.elements



       def __repr__(     self):

             """simple docstring"""





             return str(self.heap)



       def  A_   (     self):

             """simple docstring"""





             return self.elements == 0



       def  A_   (     self     ,lowercase     ,lowercase):

             """simple docstring"""





             self.heap.append((elem, weight))
             UpperCAmelCase_  :	Optional[Any]				      =		self.elements
             self.elements += 1
             self._bubble_up(lowercase)



       def  A_   (     self):

             """simple docstring"""





             if self.elements > 1:
                   self._swap_nodes(0     ,self.elements - 1)
             UpperCAmelCase_      ,      UpperCAmelCase_  :	List[str]				      =		self.heap.pop()
             del self.position_map[elem]
             self.elements -= 1
             if self.elements > 0:
                   UpperCAmelCase_      ,      UpperCAmelCase_  :	Optional[Any]				      =		self.heap[0]
                   self._bubble_down(lowercase)
             return elem



       def  A_   (     self     ,lowercase     ,lowercase):

             """simple docstring"""





             UpperCAmelCase_  :	Union[str, Any]				      =		self.position_map[elem]
             UpperCAmelCase_  :	List[Any]				      =		(elem, weight)
             if position > 0:
                   UpperCAmelCase_  :	List[str]				      =		get_parent_position(lowercase)
                   UpperCAmelCase_      ,      UpperCAmelCase_  :	Tuple				      =		self.heap[parent_position]
                   if parent_weight > weight:
                         self._bubble_up(lowercase)
                   else:
                         self._bubble_down(lowercase)
             else:
                   self._bubble_down(lowercase)



       def  A_   (     self     ,lowercase):

             """simple docstring"""





             UpperCAmelCase_  :	List[str]				      =		self.position_map[elem]
             if curr_pos == 0:
                   return None
             UpperCAmelCase_  :	Optional[int]				      =		get_parent_position(lowercase)
             UpperCAmelCase_      ,      UpperCAmelCase_  :	Any				      =		self.heap[curr_pos]
             UpperCAmelCase_      ,      UpperCAmelCase_  :	Any				      =		self.heap[parent_position]
             if parent_weight > weight:
                   self._swap_nodes(lowercase     ,lowercase)
                   return self._bubble_up(lowercase)
             return None



       def  A_   (     self     ,lowercase):

             """simple docstring"""





             UpperCAmelCase_  :	Optional[int]				      =		self.position_map[elem]
             UpperCAmelCase_      ,      UpperCAmelCase_  :	str				      =		self.heap[curr_pos]
             UpperCAmelCase_  :	str				      =		get_child_left_position(lowercase)
             UpperCAmelCase_  :	List[str]				      =		get_child_right_position(lowercase)
             if child_left_position < self.elements and child_right_position < self.elements:
                   UpperCAmelCase_      ,      UpperCAmelCase_  :	Optional[int]				      =		self.heap[child_left_position]
                   UpperCAmelCase_      ,      UpperCAmelCase_  :	List[str]				      =		self.heap[child_right_position]
                   if child_right_weight < child_left_weight and child_right_weight < weight:
                         self._swap_nodes(lowercase     ,lowercase)
                         return self._bubble_down(lowercase)
             if child_left_position < self.elements:
                   UpperCAmelCase_      ,      UpperCAmelCase_  :	Union[str, Any]				      =		self.heap[child_left_position]
                   if child_left_weight < weight:
                         self._swap_nodes(lowercase     ,lowercase)
                         return self._bubble_down(lowercase)
             else:
                   return None
             if child_right_position < self.elements:
                   UpperCAmelCase_      ,      UpperCAmelCase_  :	Union[str, Any]				      =		self.heap[child_right_position]
                   if child_right_weight < weight:
                         self._swap_nodes(lowercase     ,lowercase)
                         return self._bubble_down(lowercase)
             return None



       def  A_   (     self     ,lowercase     ,lowercase):

             """simple docstring"""





             UpperCAmelCase_  :	str				      =		self.heap[nodea_pos][0]
             UpperCAmelCase_  :	Union[str, Any]				      =		self.heap[nodea_pos][0]
             UpperCAmelCase_      ,      UpperCAmelCase_  :	str				      =		(
                 self.heap[nodea_pos],
                 self.heap[nodea_pos],
             )
             UpperCAmelCase_  :	Optional[Any]				      =		nodea_pos
             UpperCAmelCase_  :	Optional[int]				      =		nodea_pos


class   snake_case_ (Generic[T]  ):




       """simple docstring"""






       def __init__(     self):

             """simple docstring"""





             UpperCAmelCase_  :	int				      =		{}
             UpperCAmelCase_  :	Tuple				      =		0



       def __repr__(     self):

             """simple docstring"""





             return str(self.connections)



       def __len__(     self):

             """simple docstring"""





             return self.nodes



       def  A_   (     self     ,lowercase):

             """simple docstring"""





             if node not in self.connections:
                   UpperCAmelCase_  :	Optional[Any]				      =		{}
                   self.nodes += 1



       def  A_   (     self     ,lowercase     ,lowercase     ,lowercase):

             """simple docstring"""





             self.add_node(lowercase)
             self.add_node(lowercase)
             UpperCAmelCase_  :	Union[str, Any]				      =		weight
             UpperCAmelCase_  :	List[str]				      =		weight







def    _snake_case (       __snake_case					,  )     ->     tuple[dict[T, int], dict[T, T | None]]:






      '''simple docstring'''







      UpperCAmelCase_  :	List[str]				      =		{node: maxsize for node in graph.connections}
      UpperCAmelCase_  :	Tuple				      =		{node: None for node in graph.connections}

      UpperCAmelCase_  :	Union[str, Any]				      =		MinPriorityQueue()
      for node, weight in dist.items():
            priority_queue.push(__SCREAMING_SNAKE_CASE					,  __SCREAMING_SNAKE_CASE    )

      if priority_queue.is_empty():
            return dist, parent

      # initialization
      UpperCAmelCase_  :	List[Any]				      =		priority_queue.extract_min()
      UpperCAmelCase_  :	Dict				      =		0
      for neighbour in graph.connections[node]:
            if dist[neighbour] > dist[node] + graph.connections[node][neighbour]:
                  UpperCAmelCase_  :	List[Any]				      =		dist[node] + graph.connections[node][neighbour]
                  priority_queue.update_key(__SCREAMING_SNAKE_CASE					,  dist[neighbour]    )
                  UpperCAmelCase_  :	Any				      =		node

    # running prim's algorithm
      while not priority_queue.is_empty():
            UpperCAmelCase_  :	Optional[int]				      =		priority_queue.extract_min()
            for neighbour in graph.connections[node]:
                  if dist[neighbour] > dist[node] + graph.connections[node][neighbour]:
                        UpperCAmelCase_  :	Union[str, Any]				      =		dist[node] + graph.connections[node][neighbour]
                        priority_queue.update_key(__SCREAMING_SNAKE_CASE					,  dist[neighbour]    )
                        UpperCAmelCase_  :	str				      =		node
      return dist, parent





def    _snake_case (       __snake_case					,  __snake_case    )     ->     int:






      '''simple docstring'''







      return int((input_a, input_a).count(0    ) != 0    )







def    _snake_case (       )     ->     None:






      '''simple docstring'''







      assert nand_gate(0					,  0    ) == 1
      assert nand_gate(0					,  1    ) == 1
      assert nand_gate(1					,  0    ) == 1
      assert nand_gate(1					,  1    ) == 0


if __name__ == "__main__":
   print(nand_gate(0, 0))
   print(nand_gate(0, 1))
   print(nand_gate(1, 0))
   print(nand_gate(1, 1))
