





import numpy as np
import torch
from torch.utils.data import Dataset, IterableDataset

from ..utils.generic import ModelOutput



class    _a     (				lowercase__							):






       """simple docstring"""

       def __init__(      self				:     Any							,	a				:     List[str]							,	a				:     List[str]							,	a				:     Union[str, Any]    )	->Optional[Any]:
             SCREAMING_SNAKE_CASE__     :				Any   =					dataset
             SCREAMING_SNAKE_CASE__     :				List[Any]   =					process
             SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					params

       def __len__(      self				:     int    )	->Optional[int]:
             return len(self.dataset    )






       def __getitem__(      self				:     Optional[Any]							,	a				:     Tuple    )	->Dict:
             SCREAMING_SNAKE_CASE__     :				Tuple   =					self.dataset[i]
             SCREAMING_SNAKE_CASE__     :				str   =					self.process(a							,	**self.params    )
             return processed







class    _a     (				lowercase__							):






       """simple docstring"""

       def __init__(      self				:     Union[str, Any]							,	a				:     Any							,	a				:     List[Any]							,	a				:     List[Any]							,	a				:     Any=None    )	->str:
             SCREAMING_SNAKE_CASE__     :				Any   =					loader
             SCREAMING_SNAKE_CASE__     :				Optional[int]   =					infer
             SCREAMING_SNAKE_CASE__     :				List[str]   =					params
             if loader_batch_size == 1:
                   # Let's spare some time by deactivating altogether
                   SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					None
             SCREAMING_SNAKE_CASE__     :				int   =					loader_batch_size

             # Internal bookkeeping
             SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					None
             SCREAMING_SNAKE_CASE__     :				Tuple   =					None

       def __len__(      self				:     Optional[int]    )	->Optional[Any]:
             return len(self.loader    )

       def __iter__(      self				:     Any    )	->Tuple:
             SCREAMING_SNAKE_CASE__     :				List[str]   =					iter(self.loader    )
             return self

       def      A_  (      self				:     Tuple    )	->Dict:
             if isinstance(self._loader_batch_data							,	torch.Tensor    ):
                   # Batch data is simple tensor, just fetch the slice
                   SCREAMING_SNAKE_CASE__     :				int   =					self._loader_batch_data[self._loader_batch_index]
             else:
                   # Batch data is assumed to be BaseModelOutput (or dict)
                   SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					{}
                   for k, element in self._loader_batch_data.items():
                         if isinstance(a							,	a    ):
                               # Convert ModelOutput to tuple first
                               SCREAMING_SNAKE_CASE__     :				int   =					element.to_tuple()
                               if isinstance(element[0]							,	torch.Tensor    ):
                                     SCREAMING_SNAKE_CASE__     :				str   =					tuple(el[self._loader_batch_index].unsqueeze(0    ) for el in element    )
                               elif isinstance(element[0]							,	np.ndarray    ):
                                     SCREAMING_SNAKE_CASE__     :				Dict   =					tuple(np.expand_dims(el[self._loader_batch_index]							,	0    ) for el in element    )
                               continue
                         if k in {"hidden_states", "past_key_values", "attentions"} and isinstance(a							,	a    ):
                               # Those are stored as lists of tensors so need specific unbatching.
                               if isinstance(element[0]							,	torch.Tensor    ):
                                     SCREAMING_SNAKE_CASE__     :				Any   =					tuple(el[self._loader_batch_index].unsqueeze(0    ) for el in element    )
                               elif isinstance(element[0]							,	np.ndarray    ):
                                     SCREAMING_SNAKE_CASE__     :				int   =					tuple(np.expand_dims(el[self._loader_batch_index]							,	0    ) for el in element    )
                               continue
                         if element is None:
                               # This can happen for optional data that get passed around
                               SCREAMING_SNAKE_CASE__     :				Tuple   =					None
                         elif isinstance(element[self._loader_batch_index]							,	torch.Tensor    ):
                               # Take correct batch data, but make it looked like batch_size=1
                               # For compatibility with other methods within transformers

                               SCREAMING_SNAKE_CASE__     :				Optional[int]   =					element[self._loader_batch_index].unsqueeze(0    )
                         elif isinstance(element[self._loader_batch_index]							,	np.ndarray    ):
                               # Take correct batch data, but make it looked like batch_size=1
                               # For compatibility with other methods within transformers
                               SCREAMING_SNAKE_CASE__     :				Tuple   =					np.expand_dims(element[self._loader_batch_index]							,	0    )
                         else:
                               # This is typically a list, so no need to `unsqueeze`.
                               SCREAMING_SNAKE_CASE__     :				int   =					element[self._loader_batch_index]
            # Recreate the element by reusing the original class to make it look
            # batch_size=1
                   SCREAMING_SNAKE_CASE__     :				Any   =					self._loader_batch_data.__class__(a    )
             self._loader_batch_index += 1
             return result






       def      A_  (      self				:     List[str]    )	->Optional[Any]:
             if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size:
                   # We are currently unrolling a batch so we just need to return
                   # the current item within a batch
                   return self.loader_batch_item()

             # We're out of items within a batch
             SCREAMING_SNAKE_CASE__     :				Any   =					next(self.iterator    )
             SCREAMING_SNAKE_CASE__     :				List[Any]   =					self.infer(a							,	**self.params    )
             # We now have a batch of "inferred things".
             if self.loader_batch_size is not None:
                   # Try to infer the size of the batch
                   if isinstance(a							,	torch.Tensor    ):
                         SCREAMING_SNAKE_CASE__     :				Tuple   =					processed
                   else:
                         SCREAMING_SNAKE_CASE__     :				int   =					list(processed.keys()    )[0]
                         SCREAMING_SNAKE_CASE__     :				Any   =					processed[key]
                   if isinstance(a							,	a    ):
                         SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					len(a    )
                   else:
                         SCREAMING_SNAKE_CASE__     :				Optional[int]   =					first_tensor.shape[0]
                   if 0 < observed_batch_size < self.loader_batch_size:
                         # could be last batch so we can't unroll as many
                         # elements.
                         SCREAMING_SNAKE_CASE__     :				List[Any]   =					observed_batch_size
                   # Setting internal index to unwrap the batch
                   SCREAMING_SNAKE_CASE__     :				Tuple   =					processed
                   SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					0
                   return self.loader_batch_item()
             else:
                   # We're not unrolling batches
                   return processed







class    _a     (				lowercase__							):






       """simple docstring"""

       def __init__(      self				:     Tuple							,	a				:     Union[str, Any]							,	a				:     Union[str, Any]							,	a				:     List[str]							,	a				:     Any=None    )	->List[Any]:
             super().__init__(a							,	a							,	a    )

       def __iter__(      self				:     Optional[Any]    )	->Optional[Any]:
             SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					iter(self.loader    )
             SCREAMING_SNAKE_CASE__     :				Tuple   =					None
             return self






       def      A_  (      self				:     Dict    )	->int:
             if self.subiterator is None:
                   SCREAMING_SNAKE_CASE__     :				str   =					self.infer(next(self.iterator    )							,	**self.params    )
             try:
                   # Try to return next item
                   SCREAMING_SNAKE_CASE__     :				Optional[int]   =					next(self.subiterator    )
             except StopIteration:
                   # When a preprocess iterator ends, we can start lookig at the next item
                   # ChunkIterator will keep feeding until ALL elements of iterator
                   # all have created their subiterator and have been iterating against.
                   #
                   # Another way to look at it, is we're basically flattening lists of lists
                   # into a single list, but with generators
                   SCREAMING_SNAKE_CASE__     :				str   =					self.infer(next(self.iterator    )							,	**self.params    )
                   SCREAMING_SNAKE_CASE__     :				List[str]   =					next(self.subiterator    )
             return processed







class    _a     (				lowercase__							):






       """simple docstring"""

       def __iter__(      self				:     str    )	->Dict:
             SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					iter(self.loader    )
             return self






       def      A_  (      self				:     List[Any]    )	->Optional[Any]:
             # Extremely similar to PipelineIterator in its unpacking mechanism
             # BUT, we have an extra required item which is the presence of `is_last`
             # That is because everything is flattened by `PipelineChunkIterator` we
             # need to keep track of how to regroup here in the original `process`
             # boundaries so that `process` and `postprocess` see the same data.

             # This iterator accumulates items (possibly while unbatching) until it
             # its a `is_last` and then just passes it on to the caller.
             SCREAMING_SNAKE_CASE__     :				List[str]   =					False
             SCREAMING_SNAKE_CASE__     :				str   =					[]
             if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size:
                   while self._loader_batch_index < self.loader_batch_size:
                         SCREAMING_SNAKE_CASE__     :				List[str]   =					self.loader_batch_item()
                         SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					item.pop("is_last"    )
                         accumulator.append(a    )
                         if is_last:
                               return accumulator

             while not is_last:
                   SCREAMING_SNAKE_CASE__     :				Tuple   =					self.infer(next(self.iterator    )							,	**self.params    )
                   if self.loader_batch_size is not None:
                         if isinstance(a							,	torch.Tensor    ):
                               SCREAMING_SNAKE_CASE__     :				Dict   =					processed
                         else:
                               SCREAMING_SNAKE_CASE__     :				Optional[int]   =					list(processed.keys()    )[0]
                               SCREAMING_SNAKE_CASE__     :				List[str]   =					processed[key]
                         if isinstance(a							,	a    ):
                               SCREAMING_SNAKE_CASE__     :				str   =					len(a    )
                         else:
                               SCREAMING_SNAKE_CASE__     :				List[str]   =					first_tensor.shape[0]
                         if 0 < observed_batch_size < self.loader_batch_size:
                               # could be last batch so we can't unroll as many
                               # elements.
                               SCREAMING_SNAKE_CASE__     :				Any   =					observed_batch_size
                         SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					processed
                         SCREAMING_SNAKE_CASE__     :				str   =					0
                         while self._loader_batch_index < self.loader_batch_size:
                               SCREAMING_SNAKE_CASE__     :				Dict   =					self.loader_batch_item()
                               SCREAMING_SNAKE_CASE__     :				str   =					item.pop("is_last"    )
                               accumulator.append(a    )
                               if is_last:
                                     return accumulator
                   else:
                         SCREAMING_SNAKE_CASE__     :				Tuple   =					processed
                         SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					item.pop("is_last"    )
                         accumulator.append(a    )
             return accumulator







class    _a     (				lowercase__							):






       """simple docstring"""

       def __init__(      self				:     List[str]							,	a				:     Dataset							,	a				:     str    )	->Any:
             SCREAMING_SNAKE_CASE__     :				List[str]   =					dataset
             SCREAMING_SNAKE_CASE__     :				Any   =					key

       def __len__(      self				:     str    )	->List[Any]:
             return len(self.dataset    )






       def __getitem__(      self				:     int							,	a				:     Any    )	->Union[str, Any]:
             return self.dataset[i][self.key]







class    _a     (				lowercase__							):






       """simple docstring"""

       def __init__(      self				:     Tuple							,	a				:     Dataset							,	a				:     str							,	a				:     str    )	->Any:
             SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					dataset
             SCREAMING_SNAKE_CASE__     :				Optional[int]   =					keya
             SCREAMING_SNAKE_CASE__     :				List[Any]   =					keya

       def __len__(      self				:     Union[str, Any]    )	->int:
             return len(self.dataset    )






       def __getitem__(      self				:     Any							,	a				:     Union[str, Any]    )	->Any:
             return {"text": self.dataset[i][self.keya], "text_pair": self.dataset[i][self.keya]}






import sys
from collections import defaultdict



class    _a     :






							"""simple docstring"""

							def __init__(      self				:     Any    )	->Dict:
													SCREAMING_SNAKE_CASE__     :				Tuple   =					[]

							def      A_  (      self				:     int							,	a				:     List[str]    )	->Dict:
													return self.node_position[vertex]

							def      A_  (      self				:     Optional[Any]							,	a				:     Any							,	a				:     List[str]    )	->Optional[Any]:
													SCREAMING_SNAKE_CASE__     :				str   =					pos

							def      A_  (      self				:     List[Any]							,	a				:     List[str]							,	a				:     Dict							,	a				:     Dict							,	a				:     List[Any]    )	->Optional[int]:
													if start > size // 2 - 1:
																			return
													else:
																			if 2 * start + 2 >= size:
																									SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					2 * start + 1
																			else:
																									if heap[2 * start + 1] < heap[2 * start + 2]:
																															SCREAMING_SNAKE_CASE__     :				Dict   =					2 * start + 1
																									else:
																															SCREAMING_SNAKE_CASE__     :				Tuple   =					2 * start + 2
																			if heap[smallest_child] < heap[start]:
																									SCREAMING_SNAKE_CASE__,							SCREAMING_SNAKE_CASE__     :				int   =					heap[smallest_child], positions[smallest_child]
																									SCREAMING_SNAKE_CASE__,							SCREAMING_SNAKE_CASE__     :				Optional[int]   =					(
																									    heap[start],
																									    positions[start],
																									)
																									SCREAMING_SNAKE_CASE__,							SCREAMING_SNAKE_CASE__     :				Tuple   =					temp, tempa

																									SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					self.get_position(positions[smallest_child]    )
																									self.set_position(
																									    positions[smallest_child]							,	self.get_position(positions[start]    )    )
																									self.set_position(positions[start]							,	a    )

																									self.top_to_bottom(a							,	a							,	a							,	a    )

							def      A_  (      self				:     Union[str, Any]							,	a				:     Tuple							,	a				:     Tuple							,	a				:     Union[str, Any]							,	a				:     List[Any]    )	->Optional[int]:
													SCREAMING_SNAKE_CASE__     :				List[Any]   =					position[index]

													while index != 0:
																			SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					int((index - 2) / 2    ) if index % 2 == 0 else int((index - 1) / 2    )

																			if val < heap[parent]:
																									SCREAMING_SNAKE_CASE__     :				List[Any]   =					heap[parent]
																									SCREAMING_SNAKE_CASE__     :				str   =					position[parent]
																									self.set_position(position[parent]							,	a    )
																			else:
																									SCREAMING_SNAKE_CASE__     :				int   =					val
																									SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					temp
																									self.set_position(a							,	a    )
																									break
																			SCREAMING_SNAKE_CASE__     :				Optional[int]   =					parent
													else:
																			SCREAMING_SNAKE_CASE__     :				int   =					val
																			SCREAMING_SNAKE_CASE__     :				List[str]   =					temp
																			self.set_position(a							,	0    )

							def      A_  (      self				:     Union[str, Any]							,	a				:     int							,	a				:     List[str]    )	->Union[str, Any]:
													SCREAMING_SNAKE_CASE__     :				List[str]   =					len(a    ) // 2 - 1
													for i in range(a							,	-1							,	-1    ):
																			self.top_to_bottom(a							,	a							,	len(a    )							,	a    )






							def      A_  (      self				:     Dict							,	a				:     List[Any]							,	a				:     Dict    )	->Optional[int]:
													SCREAMING_SNAKE_CASE__     :				Any   =					positions[0]
													SCREAMING_SNAKE_CASE__     :				Optional[int]   =					sys.maxsize
													self.top_to_bottom(a							,	0							,	len(a    )							,	a    )
													return temp







def       UpperCAmelCase							(     _lowerCamelCase      :  str     ):





						'''simple docstring'''


						SCREAMING_SNAKE_CASE__     :				Optional[int]   =					Heap()

						SCREAMING_SNAKE_CASE__     :				Any   =					[0] * len(_lowerCamelCase     )
						SCREAMING_SNAKE_CASE__     :				Any   =					[-1] * len(_lowerCamelCase     )  # Neighboring Tree Vertex of selected vertex
						# Minimum Distance of explored vertex with neighboring vertex of partial tree
						# formed in graph
						SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					[]  # Heap of Distance of vertices from their neighboring vertex
						SCREAMING_SNAKE_CASE__     :				str   =					[]

						for vertex in range(len(_lowerCamelCase     )     ):
												distance_tv.append(sys.maxsize     )
												positions.append(_lowerCamelCase     )
												heap.node_position.append(_lowerCamelCase     )

						SCREAMING_SNAKE_CASE__     :				List[Any]   =					[]
						SCREAMING_SNAKE_CASE__     :				int   =					1
						SCREAMING_SNAKE_CASE__     :				int   =					sys.maxsize
						for neighbor, distance in adjacency_list[0]:
												SCREAMING_SNAKE_CASE__     :				int   =					0
												SCREAMING_SNAKE_CASE__     :				List[str]   =					distance
						heap.heapify(_lowerCamelCase			,   _lowerCamelCase     )

						for _ in range(1			,   len(_lowerCamelCase     )     ):
												SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					heap.delete_minimum(_lowerCamelCase			,   _lowerCamelCase     )
												if visited[vertex] == 0:
																		tree_edges.append((nbr_tv[vertex], vertex)     )
																		SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					1
																		for neighbor, distance in adjacency_list[vertex]:
																								if (
																								    visited[neighbor] == 0
																								    and distance < distance_tv[heap.get_position(_lowerCamelCase     )]
																								):
																														SCREAMING_SNAKE_CASE__     :				Any   =					distance
																														heap.bottom_to_top(
																														    _lowerCamelCase			,   heap.get_position(_lowerCamelCase     )			,   _lowerCamelCase			,   _lowerCamelCase     )
																														SCREAMING_SNAKE_CASE__     :				str   =					vertex
						return tree_edges


if __name__ == "__main__":  # pragma: no cover
				# < --------- Prims Algorithm --------- >
				__lowercase       :Union[str, Any]       =  int(input("Enter number of edges: ").strip())
				__lowercase       :Dict       =  defaultdict(list)
				for _ in range(edges_number):
								__lowercase       :Any       =  [int(x) for x in input().strip().split()]
								adjacency_list[edge[0]].append([edge[1], edge[2]])
								adjacency_list[edge[1]].append([edge[0], edge[2]])
				print(prisms_algorithm(adjacency_list))