'''simple docstring'''


def  _lowerCamelCase   (				lowerCamelCase_ :							list[list[int]]     ,					lowerCamelCase_ :							int     ,					lowerCamelCase_ :							int     ,					lowerCamelCase_ :							list[int]       ):



     """simple docstring"""





     if graph[path[curr_ind - 1]][next_ver] == 0:
          return False

     # 2. Validate that next vertex is not already in path
     return not any(vertex == next_ver for vertex in path       )


def  _lowerCamelCase   (				lowerCamelCase_ :							list[list[int]]     ,					lowerCamelCase_ :							list[int]     ,					lowerCamelCase_ :							int       ):



     """simple docstring"""





     if curr_ind == len(a_       ):
          # return whether path exists between current and starting vertices
          return graph[path[curr_ind - 1]][path[0]] == 1

     # Recursive Step
     for next_ver in range(0     ,					len(a_       )       ):
          if valid_connection(a_     ,					a_     ,					a_     ,					a_       ):
               # Insert current vertex  into path as next transition
               UpperCAmelCase_							:       Optional[Any]       =				next_ver
               # Validate created path
               if util_hamilton_cycle(a_     ,					a_     ,					curr_ind + 1       ):
                    return True
               # Backtrack
               UpperCAmelCase_							:       Optional[int]       =				-1
     return False





def  _lowerCamelCase   (				lowerCamelCase_ :							list[list[int]]     ,					lowerCamelCase_ :							int = 0       ):



     """simple docstring"""





     UpperCAmelCase_							:       int       =				[-1] * (len(a_       ) + 1)
     # initialize start and end of path with starting index
     UpperCAmelCase_							:       Union[str, Any]       =				start_index
     # evaluate and if we find answer return path either return empty array
     return path if util_hamilton_cycle(a_     ,					a_     ,					1       ) else []







'''simple docstring'''


from __future__ import annotations

from collections import deque
from collections.abc import Iterator
from dataclasses import dataclass






@dataclass
class 					__SCREAMING_SNAKE_CASE						:







  '''simple docstring'''






  lowerCamelCase_		:int
  lowerCamelCase_		:int







class 					__SCREAMING_SNAKE_CASE						:







  '''simple docstring'''






  def __init__(			self				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       list[list[Edge]]       =				[[] for _ in range(snake_case_						)]
       UpperCAmelCase_							:       Any       =				size


  def __getitem__(			self				,		snake_case_						):




       '''simple docstring'''

       return iter(self._graph[vertex]						)


  @property
  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       return self._size


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       if weight not in (0, 1):
            raise ValueError('Edge weight must be either 0 or 1.'						)

       if to_vertex < 0 or to_vertex >= self.size:
            raise ValueError('Vertex indexes must be in [0; size).'						)

       self._graph[from_vertex].append(Edge(snake_case_				,		snake_case_						)						)






  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       Optional[int]       =				deque([start_vertex]						)
       UpperCAmelCase_							:       list[int | None]       =				[None] * self.size
       UpperCAmelCase_							:       str       =				0

       while queue:
            UpperCAmelCase_							:       Optional[int]       =				queue.popleft()
            UpperCAmelCase_							:       List[str]       =				distances[current_vertex]
            if current_distance is None:
                 continue

            for edge in self[current_vertex]:
                 UpperCAmelCase_							:       Union[str, Any]       =				current_distance + edge.weight
                 UpperCAmelCase_							:       Union[str, Any]       =				distances[edge.destination_vertex]
                 if (
                     isinstance(snake_case_				,		snake_case_						)
                     and new_distance >= dest_vertex_distance
                 ):
                      continue
                 UpperCAmelCase_							:       Optional[Any]       =				new_distance
                 if edge.weight == 0:
                      queue.appendleft(edge.destination_vertex						)
                 else:
                      queue.append(edge.destination_vertex						)

       if distances[finish_vertex] is None:
            raise ValueError('No path from start_vertex to finish_vertex.'						)

       return distances[finish_vertex]


if __name__ == "__main__":
 import doctest

 doctest.testmod()






