




from __future__ import annotations

from typing import Generic, TypeVar

_lowerCamelCase      :  str      						=						TypeVar("T")
class        __snake_case  (Generic[T]		):



				def __init__(      self      :		List[Any]		,	_UpperCAmelCase      :		T      )  ->    Optional[Any]:





								'''simple docstring'''




								_lowerCAmelCase    :    Any										=      data
								_lowerCAmelCase    :    Tuple										=      self
								_lowerCAmelCase    :    Union[str, Any]										=      0






class        __snake_case  (Generic[T]		):



				def __init__(      self      :		str      )  ->    Any:





								'''simple docstring'''




								_lowerCAmelCase    :    List[str]										=      {}



				def       SCREAMING_SNAKE_CASE       (      self      :		List[Any]		,	_UpperCAmelCase      :		T      )  ->    str:





								'''simple docstring'''




								_lowerCAmelCase    :    Optional[Any]										=      DisjointSetTreeNode(__lowercase      )



				def       SCREAMING_SNAKE_CASE       (      self      :		Optional[Any]		,	_UpperCAmelCase      :		T      )  ->    Any:





								'''simple docstring'''




								_lowerCAmelCase    :    Dict										=      self.map[data]
								if elem_ref != elem_ref.parent:
												_lowerCAmelCase    :    Any										=      self.find_set(elem_ref.parent.data      )
								return elem_ref.parent



				def       SCREAMING_SNAKE_CASE       (      self      :		List[str]		,	_UpperCAmelCase      :		DisjointSetTreeNode[T]		,	_UpperCAmelCase      :		DisjointSetTreeNode[T]      )  ->    str:





								'''simple docstring'''




								if nodea.rank > nodea.rank:
												_lowerCAmelCase    :    Optional[Any]										=      nodea
								else:
												_lowerCAmelCase    :    Any										=      nodea
												if nodea.rank == nodea.rank:
																nodea.rank += 1



				def       SCREAMING_SNAKE_CASE       (      self      :		List[str]		,	_UpperCAmelCase      :		T		,	_UpperCAmelCase      :		T      )  ->    Tuple:





								'''simple docstring'''




								self.link(self.find_set(__lowercase      )		,	self.find_set(__lowercase      )      )






class        __snake_case  (Generic[T]		):



				def __init__(      self      :		int      )  ->    Dict:





								'''simple docstring'''




								_lowerCAmelCase    :    Optional[int]										=      {}



				def       SCREAMING_SNAKE_CASE       (      self      :		Optional[Any]		,	_UpperCAmelCase      :		T      )  ->    List[Any]:





								'''simple docstring'''




								if node not in self.connections:
												_lowerCAmelCase    :    List[Any]										=      {}



				def       SCREAMING_SNAKE_CASE       (      self      :		List[str]		,	_UpperCAmelCase      :		T		,	_UpperCAmelCase      :		T		,	_UpperCAmelCase      :		int      )  ->    str:





								'''simple docstring'''




								self.add_node(__lowercase      )
								self.add_node(__lowercase      )
								_lowerCAmelCase    :    Union[str, Any]										=      weight
								_lowerCAmelCase    :    List[str]										=      weight



				def       SCREAMING_SNAKE_CASE       (      self      :		Dict      )  ->    Optional[int]:





								'''simple docstring'''




								_lowerCAmelCase    :    Dict										=      []
								_lowerCAmelCase    :    Union[str, Any]										=      set()
								for start in self.connections:
												for end in self.connections[start]:
																if (start, end) not in seen:
																				seen.add((end, start)      )
																				edges.append((start, end, self.connections[start][end])      )
								edges.sort(key=lambda _UpperCAmelCase      : x[2]      )

								# creating the disjoint set
								_lowerCAmelCase    :    Any										=      DisjointSetTree[T]()
								for node in self.connections:
												disjoint_set.make_set(__lowercase      )

								# MST generation
								_lowerCAmelCase    :    Dict										=      0
								_lowerCAmelCase    :    Union[str, Any]										=      0
								_lowerCAmelCase    :    Union[str, Any]										=      GraphUndirectedWeighted[T]()
								while num_edges < len(self.connections      ) - 1:
												_lowerCAmelCase					,    _lowerCAmelCase					,    _lowerCAmelCase    :    int										=      edges[index]
												index += 1
												_lowerCAmelCase    :    Union[str, Any]										=      disjoint_set.find_set(__lowercase      )
												_lowerCAmelCase    :    Tuple										=      disjoint_set.find_set(__lowercase      )
												if parent_u != parent_v:
																num_edges += 1
																graph.add_edge(__lowercase		,	__lowercase		,	__lowercase      )
																disjoint_set.union(__lowercase		,	__lowercase      )
								return graph







from __future__ import annotations

from decimal import Decimal

from numpy import array




def 							_UpperCAmelCase  (UpperCamelCase_ : list[list[float]]       ):
				'''simple docstring'''
				_lowerCAmelCase    :    int										=      Decimal

				# Check if the provided matrix has 2 rows and 2 columns
				# since this implementation only works for 2x2 matrices
				if len(UpperCamelCase_       ) == 2 and len(matrix[0]       ) == 2 and len(matrix[1]       ) == 2:
								# Calculate the determinant of the matrix
								_lowerCAmelCase    :    Optional[Any]										=      float(
								    d(matrix[0][0]       ) * d(matrix[1][1]       ) - d(matrix[1][0]       ) * d(matrix[0][1]       )       )
								if determinant == 0:
												raise ValueError("""This matrix has no inverse."""       )

								# Creates a copy of the matrix with swapped positions of the elements
								_lowerCAmelCase    :    Union[str, Any]										=      [[0.0, 0.0], [0.0, 0.0]]
								_lowerCAmelCase					,    _lowerCAmelCase    :    Any										=      matrix[1][1], matrix[0][0]
								_lowerCAmelCase					,    _lowerCAmelCase    :    Optional[Any]										=      -matrix[1][0], -matrix[0][1]

								# Calculate the inverse of the matrix
								return [
								    [(float(d(UpperCamelCase_       )       ) / determinant) or 0.0 for n in row] for row in swapped_matrix
								]
				elif (
				    len(UpperCamelCase_       ) == 3
				    and len(matrix[0]       ) == 3
				    and len(matrix[1]       ) == 3
				    and len(matrix[2]       ) == 3
				):
								# Calculate the determinant of the matrix using Sarrus rule
								_lowerCAmelCase    :    List[str]										=      float(
								    (
								        (d(matrix[0][0]       ) * d(matrix[1][1]       ) * d(matrix[2][2]       ))
								        + (d(matrix[0][1]       ) * d(matrix[1][2]       ) * d(matrix[2][0]       ))
								        + (d(matrix[0][2]       ) * d(matrix[1][0]       ) * d(matrix[2][1]       ))
								    )
								    - (
								        (d(matrix[0][2]       ) * d(matrix[1][1]       ) * d(matrix[2][0]       ))
								        + (d(matrix[0][1]       ) * d(matrix[1][0]       ) * d(matrix[2][2]       ))
								        + (d(matrix[0][0]       ) * d(matrix[1][2]       ) * d(matrix[2][1]       ))
								    )       )
								if determinant == 0:
												raise ValueError("""This matrix has no inverse."""       )

								# Creating cofactor matrix
								_lowerCAmelCase    :    Optional[Any]										=      [
								    [d(0.0       ), d(0.0       ), d(0.0       )],
								    [d(0.0       ), d(0.0       ), d(0.0       )],
								    [d(0.0       ), d(0.0       ), d(0.0       )],
								]
								_lowerCAmelCase    :    Optional[Any]										=      (d(matrix[1][1]       ) * d(matrix[2][2]       )) - (
								    d(matrix[1][2]       ) * d(matrix[2][1]       )
								)
								_lowerCAmelCase    :    str										=      -(
								    (d(matrix[1][0]       ) * d(matrix[2][2]       )) - (d(matrix[1][2]       ) * d(matrix[2][0]       ))
								)
								_lowerCAmelCase    :    Any										=      (d(matrix[1][0]       ) * d(matrix[2][1]       )) - (
								    d(matrix[1][1]       ) * d(matrix[2][0]       )
								)
								_lowerCAmelCase    :    List[str]										=      -(
								    (d(matrix[0][1]       ) * d(matrix[2][2]       )) - (d(matrix[0][2]       ) * d(matrix[2][1]       ))
								)
								_lowerCAmelCase    :    Tuple										=      (d(matrix[0][0]       ) * d(matrix[2][2]       )) - (
								    d(matrix[0][2]       ) * d(matrix[2][0]       )
								)
								_lowerCAmelCase    :    Any										=      -(
								    (d(matrix[0][0]       ) * d(matrix[2][1]       )) - (d(matrix[0][1]       ) * d(matrix[2][0]       ))
								)
								_lowerCAmelCase    :    int										=      (d(matrix[0][1]       ) * d(matrix[1][2]       )) - (
								    d(matrix[0][2]       ) * d(matrix[1][1]       )
								)
								_lowerCAmelCase    :    Optional[Any]										=      -(
								    (d(matrix[0][0]       ) * d(matrix[1][2]       )) - (d(matrix[0][2]       ) * d(matrix[1][0]       ))
								)
								_lowerCAmelCase    :    Tuple										=      (d(matrix[0][0]       ) * d(matrix[1][1]       )) - (
								    d(matrix[0][1]       ) * d(matrix[1][0]       )
								)

								# Transpose the cofactor matrix (Adjoint matrix)
								_lowerCAmelCase    :    List[str]										=      array(UpperCamelCase_       )
								for i in range(3       ):
												for j in range(3       ):
																_lowerCAmelCase    :    List[Any]										=      cofactor_matrix[j][i]

        # Inverse of the matrix using the formula (1/determinant) * adjoint matrix
								_lowerCAmelCase    :    Tuple										=      array(UpperCamelCase_       )
								for i in range(3       ):
												for j in range(3       ):
																inverse_matrix[i][j] /= d(UpperCamelCase_       )

        # Calculate the inverse of the matrix
								return [[float(d(UpperCamelCase_       )       ) or 0.0 for n in row] for row in inverse_matrix]
				raise ValueError("""Please provide a matrix of size 2x2 or 3x3."""       )

