





from __future__ import annotations

import math
import random
from collections.abc import Collection
from typing import overload



class 					A__   :





      def __init__( self       ,							A_ = None   ):
       '''simple docstring'''




       if components is None:
        UpperCamelCase	:					Any       	=   []
       UpperCamelCase	:					List[str]       	=   list(A_   )





      def __len__( self   ):
       '''simple docstring'''




       return len(self.__components   )





      def __str__( self   ):
       '''simple docstring'''




       return "(" + ",".join(map(A_       ,							self.__components   )   ) + ")"





      def __add__( self       ,							A_   ):
       '''simple docstring'''




       UpperCamelCase	:					Optional[int]       	=   len(self   )
       if size == len(A_   ):
        UpperCamelCase	:					Optional[Any]       	=   [self.__components[i] + other.component(A_   ) for i in range(A_   )]
        return Vector(A_   )
       else:
        raise Exception("must have the same size"   )





      def __sub__( self       ,							A_   ):
       '''simple docstring'''




       UpperCamelCase	:					Tuple       	=   len(self   )
       if size == len(A_   ):
        UpperCamelCase	:					Tuple       	=   [self.__components[i] - other.component(A_   ) for i in range(A_   )]
        return Vector(A_   )
       else:  # error case
        raise Exception("must have the same size"   )





      @overload
      def __mul__( self       ,							A_   ):
       '''simple docstring'''




       ...





      @overload
      def __mul__( self       ,							A_   ):
       '''simple docstring'''




       ...





      def __mul__( self       ,							A_   ):
       '''simple docstring'''




       if isinstance(A_       ,							(float, int)   ):
        UpperCamelCase	:					Optional[Any]       	=   [c * other for c in self.__components]
        return Vector(A_   )
       elif isinstance(A_       ,							A_   ) and len(self   ) == len(A_   ):
        UpperCamelCase	:					Tuple       	=   len(self   )
        UpperCamelCase	:					Optional[int]       	=   [self.__components[i] * other.component(A_   ) for i in range(A_   )]
        return sum(A_   )
       else:  # error case
        raise Exception("invalid operand!"   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       return Vector(self.__components   )





      def        __UpperCamelCase( self       ,							A_   ):
       '''simple docstring'''




       if isinstance(A_       ,							A_   ) and -len(self.__components   ) <= i < len(self.__components   ):
        return self.__components[i]
       else:
        raise Exception("index out of range"   )





      def        __UpperCamelCase( self       ,							A_       ,							A_   ):
       '''simple docstring'''




       assert -len(self.__components   ) <= pos < len(self.__components   )
       UpperCamelCase	:					Any       	=   value





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       if len(self.__components   ) == 0:
        raise Exception("Vector is empty"   )
       UpperCamelCase	:					Dict       	=   [c**2 for c in self.__components]
       return math.sqrt(sum(A_   )   )





      def        __UpperCamelCase( self       ,							A_       ,							A_ = False   ):
       '''simple docstring'''




       UpperCamelCase	:					int       	=   self * other
       UpperCamelCase	:					str       	=   self.euclidean_length() * other.euclidean_length()
       if deg:
        return math.degrees(math.acos(num / den   )   )
       else:
        return math.acos(num / den   )



def 		A_				(     _lowerCAmelCase     )						-> Vector:
 assert isinstance(_lowerCAmelCase							,   _lowerCAmelCase     )
 return Vector([0] * dimension     )



def 		A_				(     _lowerCAmelCase							,   _lowerCAmelCase     )						-> Vector:
 assert isinstance(_lowerCAmelCase							,   _lowerCAmelCase     ) and (isinstance(_lowerCAmelCase							,   _lowerCAmelCase     ))
 UpperCamelCase	:					Dict       	=   [0] * dimension
 UpperCamelCase	:					str       	=   1
 return Vector(_lowerCAmelCase     )



def 		A_				(     _lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase     )						-> Vector:
 assert (
     isinstance(_lowerCAmelCase							,   _lowerCAmelCase     )
     and isinstance(_lowerCAmelCase							,   _lowerCAmelCase     )
     and (isinstance(_lowerCAmelCase							,   (int, float)     ))
 )
 return x * scalar + y



def 		A_				(     _lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase     )						-> Vector:
 random.seed(_lowerCAmelCase     )
 UpperCamelCase	:					str       	=   [random.randint(_lowerCAmelCase							,   _lowerCAmelCase     ) for _ in range(_lowerCAmelCase     )]
 return Vector(_lowerCAmelCase     )



class 					A__   :





      def __init__( self       ,							A_       ,							A_       ,							A_   ):
       '''simple docstring'''




       UpperCamelCase	:					List[str]       	=   matrix
       UpperCamelCase	:					List[str]       	=   w
       UpperCamelCase	:					Any       	=   h





      def __str__( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Dict       	=   ""
       for i in range(self.__height   ):
        ans += "|"
        for j in range(self.__width   ):
         if j < self.__width - 1:
          ans += str(self.__matrix[i][j]   ) + ","
         else:
          ans += str(self.__matrix[i][j]   ) + "|\n"
       return ans





      def __add__( self       ,							A_   ):
       '''simple docstring'''




       if self.__width == other.width() and self.__height == other.height():
        UpperCamelCase	:					Optional[Any]       	=   []
        for i in range(self.__height   ):
         UpperCamelCase	:					Optional[int]       	=   [
             self.__matrix[i][j] + other.component(A_       ,							A_   )
             for j in range(self.__width   )
         ]
         matrix.append(A_   )
        return Matrix(A_       ,							self.__width       ,							self.__height   )
       else:
        raise Exception("matrix must have the same dimension!"   )





      def __sub__( self       ,							A_   ):
       '''simple docstring'''




       if self.__width == other.width() and self.__height == other.height():
        UpperCamelCase	:					List[Any]       	=   []
        for i in range(self.__height   ):
         UpperCamelCase	:					Dict       	=   [
             self.__matrix[i][j] - other.component(A_       ,							A_   )
             for j in range(self.__width   )
         ]
         matrix.append(A_   )
        return Matrix(A_       ,							self.__width       ,							self.__height   )
       else:
        raise Exception("matrices must have the same dimension!"   )





      @overload
      def __mul__( self       ,							A_   ):
       '''simple docstring'''




       ...





      @overload
      def __mul__( self       ,							A_   ):
       '''simple docstring'''




       ...





      def __mul__( self       ,							A_   ):
       '''simple docstring'''




       if isinstance(A_       ,							A_   ):  # matrix-vector
        if len(A_   ) == self.__width:
         UpperCamelCase	:					int       	=   zero_vector(self.__height   )
         for i in range(self.__height   ):
          UpperCamelCase	:					int       	=   [
              self.__matrix[i][j] * other.component(A_   )
              for j in range(self.__width   )
          ]
          ans.change_component(A_       ,							sum(A_   )   )
         return ans
        else:
         raise Exception(
             "vector must have the same size as the "
             "number of columns of the matrix!"   )
       elif isinstance(A_       ,							(int, float)   ):  # matrix-scalar
        UpperCamelCase	:					Dict       	=   [
            [self.__matrix[i][j] * other for j in range(self.__width   )]
            for i in range(self.__height   )
        ]
        return Matrix(A_       ,							self.__width       ,							self.__height   )
       return None





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       return self.__height





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       return self.__width





      def        __UpperCamelCase( self       ,							A_       ,							A_   ):
       '''simple docstring'''




       if 0 <= x < self.__height and 0 <= y < self.__width:
        return self.__matrix[x][y]
       else:
        raise Exception("change_component: indices out of bounds"   )





      def        __UpperCamelCase( self       ,							A_       ,							A_       ,							A_   ):
       '''simple docstring'''




       if 0 <= x < self.__height and 0 <= y < self.__width:
        UpperCamelCase	:					List[str]       	=   value
       else:
        raise Exception("change_component: indices out of bounds"   )





      def        __UpperCamelCase( self       ,							A_       ,							A_   ):
       '''simple docstring'''




       if self.__height != self.__width:
        raise Exception("Matrix is not square"   )
       UpperCamelCase	:					List[Any]       	=   self.__matrix[:x] + self.__matrix[x + 1 :]
       for i in range(len(A_   )   ):
        UpperCamelCase	:					int       	=   minor[i][:y] + minor[i][y + 1 :]
       return Matrix(A_       ,							self.__width - 1       ,							self.__height - 1   ).determinant()





      def        __UpperCamelCase( self       ,							A_       ,							A_   ):
       '''simple docstring'''




       if self.__height != self.__width:
        raise Exception("Matrix is not square"   )
       if 0 <= x < self.__height and 0 <= y < self.__width:
        return (-1) ** (x + y) * self.minor(A_       ,							A_   )
       else:
        raise Exception("Indices out of bounds"   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       if self.__height != self.__width:
        raise Exception("Matrix is not square"   )
       if self.__height < 1:
        raise Exception("Matrix has no element"   )
       elif self.__height == 1:
        return self.__matrix[0][0]
       elif self.__height == 2:
        return (
            self.__matrix[0][0] * self.__matrix[1][1]
            - self.__matrix[0][1] * self.__matrix[1][0]
        )
       else:
        UpperCamelCase	:					Union[str, Any]       	=   [
            self.__matrix[0][y] * self.cofactor(0       ,							A_   ) for y in range(self.__width   )
        ]
        return sum(A_   )



def 		A_				(     _lowerCAmelCase     )						-> Matrix:
 UpperCamelCase	:					list[list[float]]       	=   [[0] * n for _ in range(_lowerCAmelCase     )]
 return Matrix(_lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase     )



def 		A_				(     _lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase     )						-> Matrix:
 random.seed(_lowerCAmelCase     )
 UpperCamelCase	:					list[list[float]]       	=   [
     [random.randint(_lowerCAmelCase							,   _lowerCAmelCase     ) for _ in range(_lowerCAmelCase     )] for _ in range(_lowerCAmelCase     )
 ]
 return Matrix(_lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase     )







class 					A__   :





      def __init__( self       ,							A_   ):
       '''simple docstring'''




       UpperCamelCase	:					Union[str, Any]       	=   set_counts
       UpperCamelCase	:					int       	=   max(A_   )
       UpperCamelCase	:					Optional[Any]       	=   len(A_   )
       UpperCamelCase	:					Union[str, Any]       	=   [1] * num_sets
       UpperCamelCase	:					Union[str, Any]       	=   list(range(A_   )   )





      def        __UpperCamelCase( self       ,							A_       ,							A_   ):
       '''simple docstring'''




       UpperCamelCase	:					Any       	=   self.get_parent(A_   )
       UpperCamelCase	:					Optional[int]       	=   self.get_parent(A_   )

       if src_parent == dst_parent:
        return False

       if self.ranks[dst_parent] >= self.ranks[src_parent]:
        self.set_counts[dst_parent] += self.set_counts[src_parent]
        UpperCamelCase	:					int       	=   0
        UpperCamelCase	:					Dict       	=   dst_parent
        if self.ranks[dst_parent] == self.ranks[src_parent]:
         self.ranks[dst_parent] += 1
        UpperCamelCase	:					Optional[int]       	=   self.set_counts[dst_parent]
       else:
        self.set_counts[src_parent] += self.set_counts[dst_parent]
        UpperCamelCase	:					Any       	=   0
        UpperCamelCase	:					Optional[int]       	=   src_parent
        UpperCamelCase	:					int       	=   self.set_counts[src_parent]

       UpperCamelCase	:					Any       	=   max(self.max_set       ,							A_   )
       return True





      def        __UpperCamelCase( self       ,							A_   ):
       '''simple docstring'''




       if self.parents[disj_set] == disj_set:
        return disj_set
       UpperCamelCase	:					Optional[int]       	=   self.get_parent(self.parents[disj_set]   )
       return self.parents[disj_set]
