

'''simple docstring'''



from collections import deque
from math import floor
from random import random
from time import time



class lowerCamelCase			:



			'''simple docstring'''






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





									'''simple docstring'''
									A__					:       int     ={}


			def lowercase__						(				self	:						List[str]		,      lowerCAmelCase_	:						str		,      lowerCAmelCase_	:						Optional[Any]		,      lowerCAmelCase_	:						List[str]=1			)      ->		int:





									'''simple docstring'''
									if self.graph.get(lowerCAmelCase_			):
															if self.graph[u].count([w, v]			) == 0:
																					self.graph[u].append([w, v]			)
									else:
															A__					:       int     =[[w, v]]
									if not self.graph.get(lowerCAmelCase_			):
															A__					:       Tuple     =[]


			def lowercase__						(				self	:						Union[str, Any]			)      ->		Optional[Any]:





									'''simple docstring'''
									return list(self.graph			)


			def lowercase__						(				self	:						str		,      lowerCAmelCase_	:						Any		,      lowerCAmelCase_	:						List[Any]			)      ->		Any:





									'''simple docstring'''
									if self.graph.get(lowerCAmelCase_			):
															for _ in self.graph[u]:
																					if _[1] == v:
																											self.graph[u].remove(lowerCAmelCase_			)


			def lowercase__						(				self	:						Tuple		,      lowerCAmelCase_	:						str=-2		,      lowerCAmelCase_	:						Dict=-1			)      ->		Optional[int]:





									'''simple docstring'''
									if s == d:
															return []
									A__					:       Union[str, Any]     =[]
									A__					:       Union[str, Any]     =[]
									if s == -2:
															A__					:       List[Any]     =list(self.graph			)[0]
									stack.append(lowerCAmelCase_			)
									visited.append(lowerCAmelCase_			)
									A__					:       Any     =s

									while True:
															# check if there is any non isolated nodes
															if len(self.graph[s]			) != 0:
																					A__					:       Optional[int]     =s
																					for node in self.graph[s]:
																											if visited.count(node[1]			) < 1:
																																	if node[1] == d:
																																							visited.append(lowerCAmelCase_			)
																																							return visited
																																	else:
																																							stack.append(node[1]			)
																																							visited.append(node[1]			)
																																							A__					:       int     =node[1]
																																							break

            # check if all the children are visited
															if s == ss:
																					stack.pop()
																					if len(lowerCAmelCase_			) != 0:
																											A__					:       str     =stack[len(lowerCAmelCase_			) - 1]
															else:
																					A__					:       Optional[Any]     =ss

															# check if se have reached the starting point
															if len(lowerCAmelCase_			) == 0:
																					return visited


			def lowercase__						(				self	:						List[str]		,      lowerCAmelCase_	:						Optional[Any]=-1			)      ->		List[str]:





									'''simple docstring'''
									if c == -1:
															A__					:       Tuple     =floor(random() * 1_00_00			) + 10
									for i in range(lowerCAmelCase_			):
															# every vertex has max 100 edges
															for _ in range(floor(random() * 1_02			) + 1			):
																					A__					:       Dict     =floor(random() * c			) + 1
																					if n != i:
																											self.add_pair(lowerCAmelCase_		,      lowerCAmelCase_		,      1			)


			def lowercase__						(				self	:						str		,      lowerCAmelCase_	:						Any=-2			)      ->		Tuple:





									'''simple docstring'''
									A__					:       Optional[Any]     =deque()
									A__					:       List[Any]     =[]
									if s == -2:
															A__					:       int     =list(self.graph			)[0]
									d.append(lowerCAmelCase_			)
									visited.append(lowerCAmelCase_			)
									while d:
															A__					:       Dict     =d.popleft()
															if len(self.graph[s]			) != 0:
																					for node in self.graph[s]:
																											if visited.count(node[1]			) < 1:
																																	d.append(node[1]			)
																																	visited.append(node[1]			)
									return visited


			def lowercase__						(				self	:						List[Any]		,      lowerCAmelCase_	:						List[Any]			)      ->		str:





									'''simple docstring'''
									A__					:       Tuple     =0
									for x in self.graph:
															for y in self.graph[x]:
																					if y[1] == u:
																											count += 1
									return count


			def lowercase__						(				self	:						Dict		,      lowerCAmelCase_	:						Union[str, Any]			)      ->		Union[str, Any]:





									'''simple docstring'''
									return len(self.graph[u]			)


			def lowercase__						(				self	:						Optional[Any]		,      lowerCAmelCase_	:						Union[str, Any]=-2			)      ->		Optional[int]:





									'''simple docstring'''
									A__					:       List[Any]     =[]
									A__					:       int     =[]
									if s == -2:
															A__					:       int     =list(self.graph			)[0]
									stack.append(lowerCAmelCase_			)
									visited.append(lowerCAmelCase_			)
									A__					:       Dict     =s
									A__					:       Optional[int]     =[]

									while True:
															# check if there is any non isolated nodes
															if len(self.graph[s]			) != 0:
																					A__					:       Any     =s
																					for node in self.graph[s]:
																											if visited.count(node[1]			) < 1:
																																	stack.append(node[1]			)
																																	visited.append(node[1]			)
																																	A__					:       Any     =node[1]
																																	break

            # check if all the children are visited
															if s == ss:
																					sorted_nodes.append(stack.pop()			)
																					if len(lowerCAmelCase_			) != 0:
																											A__					:       Any     =stack[len(lowerCAmelCase_			) - 1]
															else:
																					A__					:       List[str]     =ss

															# check if se have reached the starting point
															if len(lowerCAmelCase_			) == 0:
																					return sorted_nodes


			def lowercase__						(				self	:						Any			)      ->		Tuple:





									'''simple docstring'''
									A__					:       List[str]     =[]
									A__					:       Dict     =[]
									A__					:       List[Any]     =list(self.graph			)[0]
									stack.append(lowerCAmelCase_			)
									visited.append(lowerCAmelCase_			)
									A__					:       List[Any]     =-2
									A__					:       Dict     =[]
									A__					:       List[Any]     =s
									A__					:       str     =False
									A__					:       Optional[int]     =set()

									while True:
															# check if there is any non isolated nodes
															if len(self.graph[s]			) != 0:
																					A__					:       Optional[Any]     =s
																					for node in self.graph[s]:
																											if (
																											    visited.count(node[1]			) > 0
																											    and node[1] != parent
																											    and indirect_parents.count(node[1]			) > 0
																											    and not on_the_way_back
																											):
																																	A__					:       List[str]     =len(lowerCAmelCase_			) - 1
																																	while len_stack >= 0:
																																							if stack[len_stack] == node[1]:
																																													anticipating_nodes.add(node[1]			)
																																													break
																																							else:
																																													anticipating_nodes.add(stack[len_stack]			)
																																													len_stack -= 1
																											if visited.count(node[1]			) < 1:
																																	stack.append(node[1]			)
																																	visited.append(node[1]			)
																																	A__					:       List[Any]     =node[1]
																																	break

            # check if all the children are visited
															if s == ss:
																					stack.pop()
																					A__					:       Any     =True
																					if len(lowerCAmelCase_			) != 0:
																											A__					:       Dict     =stack[len(lowerCAmelCase_			) - 1]
															else:
																					A__					:       Any     =False
																					indirect_parents.append(lowerCAmelCase_			)
																					A__					:       str     =s
																					A__					:       Optional[Any]     =ss

															# check if se have reached the starting point
															if len(lowerCAmelCase_			) == 0:
																					return list(lowerCAmelCase_			)


			def lowercase__						(				self	:						str			)      ->		Optional[Any]:





									'''simple docstring'''
									A__					:       Any     =[]
									A__					:       Optional[int]     =[]
									A__					:       Union[str, Any]     =list(self.graph			)[0]
									stack.append(lowerCAmelCase_			)
									visited.append(lowerCAmelCase_			)
									A__					:       Union[str, Any]     =-2
									A__					:       Any     =[]
									A__					:       str     =s
									A__					:       Optional[Any]     =False
									A__					:       Tuple     =set()

									while True:
															# check if there is any non isolated nodes
															if len(self.graph[s]			) != 0:
																					A__					:       str     =s
																					for node in self.graph[s]:
																											if (
																											    visited.count(node[1]			) > 0
																											    and node[1] != parent
																											    and indirect_parents.count(node[1]			) > 0
																											    and not on_the_way_back
																											):
																																	A__					:       List[str]     =len(lowerCAmelCase_			) - 1
																																	while len_stack_minus_one >= 0:
																																							if stack[len_stack_minus_one] == node[1]:
																																													anticipating_nodes.add(node[1]			)
																																													break
																																							else:
																																													return True
																											if visited.count(node[1]			) < 1:
																																	stack.append(node[1]			)
																																	visited.append(node[1]			)
																																	A__					:       List[str]     =node[1]
																																	break

            # check if all the children are visited
															if s == ss:
																					stack.pop()
																					A__					:       Optional[int]     =True
																					if len(lowerCAmelCase_			) != 0:
																											A__					:       Optional[int]     =stack[len(lowerCAmelCase_			) - 1]
															else:
																					A__					:       List[str]     =False
																					indirect_parents.append(lowerCAmelCase_			)
																					A__					:       List[str]     =s
																					A__					:       Tuple     =ss

															# check if se have reached the starting point
															if len(lowerCAmelCase_			) == 0:
																					return False


			def lowercase__						(				self	:						Optional[Any]		,      lowerCAmelCase_	:						Optional[int]=-2		,      lowerCAmelCase_	:						Union[str, Any]=-1			)      ->		int:





									'''simple docstring'''
									A__					:       Tuple     =time()
									self.dfs(lowerCAmelCase_		,      lowerCAmelCase_			)
									A__					:       int     =time()
									return end - begin



			def lowercase__						(				self	:						Union[str, Any]		,      lowerCAmelCase_	:						Tuple=-2			)      ->		int:





									'''simple docstring'''
									A__					:       Union[str, Any]     =time()
									self.bfs(lowerCAmelCase_			)
									A__					:       List[Any]     =time()
									return end - begin



class lowerCamelCase			:



			'''simple docstring'''






			def __init__(				self	:						int			)      ->		Tuple:





									'''simple docstring'''
									A__					:       str     ={}


			def lowercase__						(				self	:						Tuple		,      lowerCAmelCase_	:						Any		,      lowerCAmelCase_	:						List[str]		,      lowerCAmelCase_	:						List[Any]=1			)      ->		Optional[int]:





									'''simple docstring'''
									# check if the u exists
									if self.graph.get(lowerCAmelCase_			):
															# if there already is a edge
															if self.graph[u].count([w, v]			) == 0:
																					self.graph[u].append([w, v]			)
									else:
															# if u does not exist
															A__					:       Union[str, Any]     =[[w, v]]
									# add the other way
									if self.graph.get(lowerCAmelCase_			):
															# if there already is a edge
															if self.graph[v].count([w, u]			) == 0:
																					self.graph[v].append([w, u]			)
									else:
															# if u does not exist
															A__					:       Any     =[[w, u]]


			def lowercase__						(				self	:						Optional[int]		,      lowerCAmelCase_	:						str		,      lowerCAmelCase_	:						Any			)      ->		Dict:





									'''simple docstring'''
									if self.graph.get(lowerCAmelCase_			):
															for _ in self.graph[u]:
																					if _[1] == v:
																											self.graph[u].remove(lowerCAmelCase_			)
        # the other way round
									if self.graph.get(lowerCAmelCase_			):
															for _ in self.graph[v]:
																					if _[1] == u:
																											self.graph[v].remove(lowerCAmelCase_			)


			def lowercase__						(				self	:						Union[str, Any]		,      lowerCAmelCase_	:						int=-2		,      lowerCAmelCase_	:						int=-1			)      ->		Optional[Any]:





									'''simple docstring'''
									if s == d:
															return []
									A__					:       Any     =[]
									A__					:       List[Any]     =[]
									if s == -2:
															A__					:       List[Any]     =list(self.graph			)[0]
									stack.append(lowerCAmelCase_			)
									visited.append(lowerCAmelCase_			)
									A__					:       List[Any]     =s

									while True:
															# check if there is any non isolated nodes
															if len(self.graph[s]			) != 0:
																					A__					:       Dict     =s
																					for node in self.graph[s]:
																											if visited.count(node[1]			) < 1:
																																	if node[1] == d:
																																							visited.append(lowerCAmelCase_			)
																																							return visited
																																	else:
																																							stack.append(node[1]			)
																																							visited.append(node[1]			)
																																							A__					:       Union[str, Any]     =node[1]
																																							break

            # check if all the children are visited
															if s == ss:
																					stack.pop()
																					if len(lowerCAmelCase_			) != 0:
																											A__					:       Tuple     =stack[len(lowerCAmelCase_			) - 1]
															else:
																					A__					:       List[Any]     =ss

															# check if se have reached the starting point
															if len(lowerCAmelCase_			) == 0:
																					return visited


			def lowercase__						(				self	:						Optional[int]		,      lowerCAmelCase_	:						List[str]=-1			)      ->		Tuple:





									'''simple docstring'''
									if c == -1:
															A__					:       int     =floor(random() * 1_00_00			) + 10
									for i in range(lowerCAmelCase_			):
															# every vertex has max 100 edges
															for _ in range(floor(random() * 1_02			) + 1			):
																					A__					:       List[str]     =floor(random() * c			) + 1
																					if n != i:
																											self.add_pair(lowerCAmelCase_		,      lowerCAmelCase_		,      1			)


			def lowercase__						(				self	:						Dict		,      lowerCAmelCase_	:						List[str]=-2			)      ->		Dict:





									'''simple docstring'''
									A__					:       List[Any]     =deque()
									A__					:       Union[str, Any]     =[]
									if s == -2:
															A__					:       Dict     =list(self.graph			)[0]
									d.append(lowerCAmelCase_			)
									visited.append(lowerCAmelCase_			)
									while d:
															A__					:       Tuple     =d.popleft()
															if len(self.graph[s]			) != 0:
																					for node in self.graph[s]:
																											if visited.count(node[1]			) < 1:
																																	d.append(node[1]			)
																																	visited.append(node[1]			)
									return visited


			def lowercase__						(				self	:						Any		,      lowerCAmelCase_	:						Tuple			)      ->		Optional[Any]:





									'''simple docstring'''
									return len(self.graph[u]			)


			def lowercase__						(				self	:						Optional[Any]			)      ->		Union[str, Any]:





									'''simple docstring'''
									A__					:       Union[str, Any]     =[]
									A__					:       str     =[]
									A__					:       str     =list(self.graph			)[0]
									stack.append(lowerCAmelCase_			)
									visited.append(lowerCAmelCase_			)
									A__					:       Dict     =-2
									A__					:       Optional[Any]     =[]
									A__					:       Optional[Any]     =s
									A__					:       int     =False
									A__					:       str     =set()

									while True:
															# check if there is any non isolated nodes
															if len(self.graph[s]			) != 0:
																					A__					:       str     =s
																					for node in self.graph[s]:
																											if (
																											    visited.count(node[1]			) > 0
																											    and node[1] != parent
																											    and indirect_parents.count(node[1]			) > 0
																											    and not on_the_way_back
																											):
																																	A__					:       List[str]     =len(lowerCAmelCase_			) - 1
																																	while len_stack >= 0:
																																							if stack[len_stack] == node[1]:
																																													anticipating_nodes.add(node[1]			)
																																													break
																																							else:
																																													anticipating_nodes.add(stack[len_stack]			)
																																													len_stack -= 1
																											if visited.count(node[1]			) < 1:
																																	stack.append(node[1]			)
																																	visited.append(node[1]			)
																																	A__					:       Union[str, Any]     =node[1]
																																	break

            # check if all the children are visited
															if s == ss:
																					stack.pop()
																					A__					:       Optional[int]     =True
																					if len(lowerCAmelCase_			) != 0:
																											A__					:       Optional[Any]     =stack[len(lowerCAmelCase_			) - 1]
															else:
																					A__					:       Optional[Any]     =False
																					indirect_parents.append(lowerCAmelCase_			)
																					A__					:       Tuple     =s
																					A__					:       Tuple     =ss

															# check if se have reached the starting point
															if len(lowerCAmelCase_			) == 0:
																					return list(lowerCAmelCase_			)


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





									'''simple docstring'''
									A__					:       List[Any]     =[]
									A__					:       Dict     =[]
									A__					:       List[str]     =list(self.graph			)[0]
									stack.append(lowerCAmelCase_			)
									visited.append(lowerCAmelCase_			)
									A__					:       Dict     =-2
									A__					:       List[str]     =[]
									A__					:       Optional[Any]     =s
									A__					:       Dict     =False
									A__					:       int     =set()

									while True:
															# check if there is any non isolated nodes
															if len(self.graph[s]			) != 0:
																					A__					:       Optional[int]     =s
																					for node in self.graph[s]:
																											if (
																											    visited.count(node[1]			) > 0
																											    and node[1] != parent
																											    and indirect_parents.count(node[1]			) > 0
																											    and not on_the_way_back
																											):
																																	A__					:       Optional[Any]     =len(lowerCAmelCase_			) - 1
																																	while len_stack_minus_one >= 0:
																																							if stack[len_stack_minus_one] == node[1]:
																																													anticipating_nodes.add(node[1]			)
																																													break
																																							else:
																																													return True
																											if visited.count(node[1]			) < 1:
																																	stack.append(node[1]			)
																																	visited.append(node[1]			)
																																	A__					:       Optional[int]     =node[1]
																																	break

            # check if all the children are visited
															if s == ss:
																					stack.pop()
																					A__					:       Optional[Any]     =True
																					if len(lowerCAmelCase_			) != 0:
																											A__					:       List[str]     =stack[len(lowerCAmelCase_			) - 1]
															else:
																					A__					:       List[str]     =False
																					indirect_parents.append(lowerCAmelCase_			)
																					A__					:       Optional[Any]     =s
																					A__					:       Dict     =ss

															# check if se have reached the starting point
															if len(lowerCAmelCase_			) == 0:
																					return False


			def lowercase__						(				self	:						int			)      ->		int:





									'''simple docstring'''
									return list(self.graph			)


			def lowercase__						(				self	:						Tuple		,      lowerCAmelCase_	:						Any=-2		,      lowerCAmelCase_	:						Union[str, Any]=-1			)      ->		Tuple:





									'''simple docstring'''
									A__					:       Union[str, Any]     =time()
									self.dfs(lowerCAmelCase_		,      lowerCAmelCase_			)
									A__					:       int     =time()
									return end - begin



			def lowercase__						(				self	:						List[str]		,      lowerCAmelCase_	:						Tuple=-2			)      ->		str:





									'''simple docstring'''
									A__					:       str     =time()
									self.bfs(lowerCAmelCase_			)
									A__					:       str     =time()
									return end - begin




'''simple docstring'''



from __future__ import annotations

from typing import Any



class lowerCamelCase			:



			'''simple docstring'''






			def __init__(				self	:						List[str]		,      lowerCAmelCase_	:						int			)      ->		None:





									'''simple docstring'''
									A__					:       Any     =num_of_nodes
									A__					:       list[list[int]]     =[]
									A__					:       dict[int, int]     ={}


			def lowercase__						(				self	:						Optional[Any]		,      lowerCAmelCase_	:						int		,      lowerCAmelCase_	:						int		,      lowerCAmelCase_	:						int			)      ->		None:





									'''simple docstring'''
									self.m_edges.append([u_node, v_node, weight]			)


			def lowercase__						(				self	:						int		,      lowerCAmelCase_	:						int			)      ->		int:





									'''simple docstring'''

									if self.m_component[u_node] == u_node:
															return u_node
									return self.find_component(self.m_component[u_node]			)


			def lowercase__						(				self	:						Optional[int]		,      lowerCAmelCase_	:						int			)      ->		None:





									'''simple docstring'''

									if self.m_component[u_node] != u_node:
															for k in self.m_component:
																					A__					:       str     =self.find_component(lowerCAmelCase_			)


			def lowercase__						(				self	:						int		,      lowerCAmelCase_	:						list[int]		,      lowerCAmelCase_	:						int		,      lowerCAmelCase_	:						int			)      ->		None:





									'''simple docstring'''

									if component_size[u_node] <= component_size[v_node]:
															A__					:       int     =v_node
															component_size[v_node] += component_size[u_node]
															self.set_component(lowerCAmelCase_			)

									elif component_size[u_node] >= component_size[v_node]:
															A__					:       List[str]     =self.find_component(lowerCAmelCase_			)
															component_size[u_node] += component_size[v_node]
															self.set_component(lowerCAmelCase_			)



			def lowercase__						(				self	:						str			)      ->		None:





									'''simple docstring'''
									A__					:       Union[str, Any]     =[]
									A__					:       List[str]     =0

									A__					:       list[Any]     =[-1] * self.m_num_of_nodes

									# A list of components (initialized to all of the nodes)
									for node in range(self.m_num_of_nodes			):
															self.m_component.update({node: node}			)
															component_size.append(1			)

									A__					:       List[str]     =self.m_num_of_nodes

									while num_of_components > 1:
															for edge in self.m_edges:
																					A__   ,						A__   ,						A__					:       Any     =edge

																					A__					:       Tuple     =self.m_component[u]
																					A__					:       Optional[Any]     =self.m_component[v]

																					if u_component != v_component:

																											for component in (u_component, v_component):
																																	if (
																																	    minimum_weight_edge[component] == -1
																																	    or minimum_weight_edge[component][2] > w
																																	):
																																							A__					:       Optional[int]     =[u, v, w]

															for edge in minimum_weight_edge:
																					if isinstance(lowerCAmelCase_		,      lowerCAmelCase_			):
																											A__   ,						A__   ,						A__					:       Tuple     =edge

																											A__					:       Any     =self.m_component[u]
																											A__					:       Optional[Any]     =self.m_component[v]

																											if u_component != v_component:
																																	mst_weight += w
																																	self.union(lowerCAmelCase_		,      lowerCAmelCase_		,      lowerCAmelCase_			)
																																	print(f"Added edge [{u} - {v}]\nAdded weight: {w}\n"			)
																																	num_of_components -= 1

															A__					:       int     =[-1] * self.m_num_of_nodes
									print(f"The total weight of the minimal spanning tree is: {mst_weight}"			)


def 						__lowerCamelCase	(       )	->							None:






						"""simple docstring"""







if __name__ == "__main__":
		import doctest

		doctest.testmod()

