

import heapq as hq
import math
from collections.abc import Iterator





class A__    :



 """simple docstring"""







 def __init__(		self :	List[Any]     ,						A_ :	List[Any]       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Optional[int]        =    str(id_       )
      _lowerCAmelCase   :					Union[str, Any]        =    None
      _lowerCAmelCase   :					str        =    None
      _lowerCAmelCase   :					Union[str, Any]        =    []
      _lowerCAmelCase   :					str        =    {}  # {vertex:distance}

 def __lt__(		self :	int     ,						A_ :	Dict       ):






      '''simple docstring'''




      return self.key < other.key

 def __repr__(		self :	int       ):






      '''simple docstring'''




      return self.id

 def    __magic_name__       (		self :	Optional[Any]     ,						A_ :	Dict       ):






      '''simple docstring'''




      self.neighbors.append(A_       )


 def    __magic_name__       (		self :	List[str]     ,						A_ :	Tuple     ,						A_ :	List[Any]       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Tuple        =    weight







def 	_snake_case		(    SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE				)       ->      str:

     """simple docstring"""







     graph[a - 1].add_neighbor(graph[b - 1]				)
     graph[b - 1].add_neighbor(graph[a - 1]				)
     # add the edges:
     graph[a - 1].add_edge(graph[b - 1]		,							SCREAMING_SNAKE_CASE				)
     graph[b - 1].add_edge(graph[a - 1]		,							SCREAMING_SNAKE_CASE				)







def 	_snake_case		(    SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE				)       ->      list:

     """simple docstring"""







     _lowerCAmelCase   :					List[Any]        =    []
     for u in graph:
          _lowerCAmelCase   :					Optional[int]        =    math.inf
          _lowerCAmelCase   :					str        =    None
     _lowerCAmelCase   :					Optional[int]        =    0
     _lowerCAmelCase   :					List[Any]        =    graph[:]
     while q:
          _lowerCAmelCase   :					str        =    min(SCREAMING_SNAKE_CASE				)
          q.remove(SCREAMING_SNAKE_CASE				)
          for v in u.neighbors:
               if (v in q) and (u.edges[v.id] < v.key):
                    _lowerCAmelCase   :					int        =    u
                    _lowerCAmelCase   :					List[Any]        =    u.edges[v.id]
     for i in range(1		,							len(SCREAMING_SNAKE_CASE				)				):
          a.append((int(graph[i].id				) + 1, int(graph[i].pi.id				) + 1)				)
     return a







def 	_snake_case		(    SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE				)       ->      Iterator[tuple]:

     """simple docstring"""







     for u in graph:
          _lowerCAmelCase   :					Tuple        =    math.inf
          _lowerCAmelCase   :					Tuple        =    None
     _lowerCAmelCase   :					List[Any]        =    0

     _lowerCAmelCase   :					Union[str, Any]        =    list(SCREAMING_SNAKE_CASE				)
     hq.heapify(SCREAMING_SNAKE_CASE				)

     while h:
          _lowerCAmelCase   :					Optional[int]        =    hq.heappop(SCREAMING_SNAKE_CASE				)
          for v in u.neighbors:
               if (v in h) and (u.edges[v.id] < v.key):
                    _lowerCAmelCase   :					List[str]        =    u
                    _lowerCAmelCase   :					List[str]        =    u.edges[v.id]
                    hq.heapify(SCREAMING_SNAKE_CASE				)

     for i in range(1		,							len(SCREAMING_SNAKE_CASE				)				):
          yield (int(graph[i].id				) + 1, int(graph[i].pi.id				) + 1)







def 	_snake_case		(    )       ->      None:

     """simple docstring"""
















if __name__ == "__main__":
  import doctest

  doctest.testmod()









# Author: OMKAR PATHAK, Nwachukwu Chidiebere

# Use a Python dictionary to construct the graph.
from __future__ import annotations

from pprint import pformat
from typing import Generic, TypeVar

__UpperCAmelCase    				=     TypeVar('T')





class A__    ( Generic[T] ):



 """simple docstring"""







 def __init__(		self :	Optional[int]     ,						A_ :	bool = True       ):






      '''simple docstring'''




      _lowerCAmelCase   :					dict[T, list[T]]        =    {}  # dictionary of lists
      _lowerCAmelCase   :					List[Any]        =    directed

 def    __magic_name__       (		self :	Any     ,						A_ :	T     ,						A_ :	T       ):






      '''simple docstring'''




      if not self.directed:  # For undirected graphs
           # if both source vertex and destination vertex are both present in the
           # adjacency list, add destination vertex to source vertex list of adjacent
           # vertices and add source vertex to destination vertex list of adjacent
           # vertices.
           if source_vertex in self.adj_list and destination_vertex in self.adj_list:
                self.adj_list[source_vertex].append(A_       )
                self.adj_list[destination_vertex].append(A_       )
           # if only source vertex is present in adjacency list, add destination vertex
           # to source vertex list of adjacent vertices, then create a new vertex with
           # destination vertex as key and assign a list containing the source vertex
           # as it's first adjacent vertex.
           elif source_vertex in self.adj_list:
                self.adj_list[source_vertex].append(A_       )
                _lowerCAmelCase   :					List[Any]        =    [source_vertex]
           # if only destination vertex is present in adjacency list, add source vertex
           # to destination vertex list of adjacent vertices, then create a new vertex
           # with source vertex as key and assign a list containing the source vertex
           # as it's first adjacent vertex.
           elif destination_vertex in self.adj_list:
                self.adj_list[destination_vertex].append(A_       )
                _lowerCAmelCase   :					List[str]        =    [destination_vertex]
           # if both source vertex and destination vertex are not present in adjacency
           # list, create a new vertex with source vertex as key and assign a list
           # containing the destination vertex as it's first adjacent vertex also
           # create a new vertex with destination vertex as key and assign a list
           # containing the source vertex as it's first adjacent vertex.
           else:
                _lowerCAmelCase   :					List[str]        =    [destination_vertex]
                _lowerCAmelCase   :					Dict        =    [source_vertex]
      else:  # For directed graphs
           # if both source vertex and destination vertex are present in adjacency
           # list, add destination vertex to source vertex list of adjacent vertices.
           if source_vertex in self.adj_list and destination_vertex in self.adj_list:
                self.adj_list[source_vertex].append(A_       )
           # if only source vertex is present in adjacency list, add destination
           # vertex to source vertex list of adjacent vertices and create a new vertex
           # with destination vertex as key, which has no adjacent vertex
           elif source_vertex in self.adj_list:
                self.adj_list[source_vertex].append(A_       )
                _lowerCAmelCase   :					Tuple        =    []
           # if only destination vertex is present in adjacency list, create a new
           # vertex with source vertex as key and assign a list containing destination
           # vertex as first adjacent vertex
           elif destination_vertex in self.adj_list:
                _lowerCAmelCase   :					Dict        =    [destination_vertex]
           # if both source vertex and destination vertex are not present in adjacency
           # list, create a new vertex with source vertex as key and a list containing
           # destination vertex as it's first adjacent vertex. Then create a new vertex
           # with destination vertex as key, which has no adjacent vertex
           else:
                _lowerCAmelCase   :					Any        =    [destination_vertex]
                _lowerCAmelCase   :					Union[str, Any]        =    []

      return self


 def __repr__(		self :	List[str]       ):






      '''simple docstring'''




      return pformat(self.adj_list       )






