






import unittest
from functools import partial

import torch

from accelerate import Accelerator, debug_launcher
from accelerate.state import AcceleratorState, GradientState
from accelerate.test_utils import require_cpu, require_huggingface_suite
from accelerate.utils import GradientAccumulationPlugin







def 						a_      (__A=2							,					__A=True							,					__A=False			)							->	Dict:



					"""simple docstring"""
					__a				:	Any			=				Accelerator(step_scheduler_with_optimizer=__A							,					split_batches=__A			)
					__a				:	Any			=				torch.nn.Linear(2							,					4			)
					__a				:	Union[str, Any]			=				torch.optim.AdamW(model.parameters()							,					lr=1.0			)
					__a				:	str			=				torch.optim.lr_scheduler.OneCycleLR(__A							,					max_lr=0.01							,					steps_per_epoch=2							,					epochs=1			)
					__a			, __a			, __a				:	List[str]			=				accelerator.prepare(__A							,					__A							,					__A			)

					# Optimizer has stepped
					scheduler.step()
					if step_scheduler_with_optimizer or (num_processes == 1):
										assert (
										    scheduler.scheduler.last_epoch == num_processes
										), f'Last Epoch ({scheduler.scheduler.last_epoch}) != Num Processes ({num_processes})'
					else:
										assert (
										    scheduler.scheduler.last_epoch != num_processes
										), f'Last Epoch ({scheduler.scheduler.last_epoch}) == Num Processes ({num_processes})'







def 						a_      (__A=2							,					__A=True							,					__A=False			)							->	Union[str, Any]:



					"""simple docstring"""
					__a				:	Optional[int]			=				Accelerator(step_scheduler_with_optimizer=__A							,					split_batches=__A			)
					__a				:	Any			=				torch.nn.Linear(2							,					4			)
					__a				:	List[Any]			=				torch.optim.AdamW(model.parameters()							,					lr=1.0			)
					__a				:	List[str]			=				torch.optim.lr_scheduler.LambdaLR(__A							,					lr_lambda=lambda __A			: 1 - n / 10			)
					__a			, __a			, __a				:	List[str]			=				accelerator.prepare(__A							,					__A							,					__A			)

					# Optimizer has stepped
					__a				:	Optional[Any]			=				False
					scheduler.step()
					__a				:	int			=				1 - (num_processes if (step_scheduler_with_optimizer and not split_batches) else 1) / 10
					assert (
					    scheduler.get_last_lr()[0] == expected_lr
					), f'Wrong lr found at first step, expected {expected_lr}, got {scheduler.get_last_lr()[0]}'

					# Optimizer has not stepped
					__a				:	Optional[int]			=				True
					scheduler.step()
					if not step_scheduler_with_optimizer:
										__a				:	Optional[int]			=				1 - 2 / 10
					assert (
					    scheduler.get_last_lr()[0] == expected_lr
					), f'Wrong lr found at second step, expected {expected_lr}, got {scheduler.get_last_lr()[0]}'







def 						a_      (__A = 2			)							->	Any:



					"""simple docstring"""
					from transformers import get_linear_schedule_with_warmup

					__a				:	Union[str, Any]			=				[1, 2, 4]
					for num_steps in steps:
										__a				:	Optional[Any]			=				GradientAccumulationPlugin(num_steps=__A							,					adjust_scheduler=num_steps > 1			)
										__a				:	int			=				Accelerator(gradient_accumulation_plugin=__A			)
										__a				:	str			=				torch.nn.Linear(2							,					4			)
										__a				:	str			=				torch.optim.AdamW(model.parameters()							,					lr=10.0			)
										__a				:	Optional[int]			=				get_linear_schedule_with_warmup(optimizer=__A							,					num_warmup_steps=0							,					num_training_steps=20			)

										__a			, __a			, __a				:	List[Any]			=				accelerator.prepare(__A							,					__A							,					__A			)

										for i in range(10 * num_steps			):
															with accelerator.accumulate(__A			):
																				optimizer.step()
																				scheduler.step()

															if i == (10 * num_steps - 2):
																				assert (
																				    scheduler.get_last_lr()[0] != 0
																				), f'Wrong lr found at second-to-last step, expected non-zero, got {scheduler.get_last_lr()[0]}. num_steps: {num_steps}'
										assert (
										    scheduler.get_last_lr()[0] == 0
										), f'Wrong lr found at last step, expected 0, got {scheduler.get_last_lr()[0]}'
										GradientState._reset_state()




@require_cpu
class        snake_case_					(						unittest.TestCase			):

						"""simple docstring"""



						def 					UpperCAmelCase__				(self:				Union[str, Any]      )						->    List[Any]:




											'''simple docstring'''



											debug_launcher(partial(__UpperCAmelCase   ,				num_processes=1      )   ,				num_processes=1      )
											debug_launcher(partial(__UpperCAmelCase   ,				num_processes=1   ,				split_batches=__UpperCAmelCase      )   ,				num_processes=1      )



						def 					UpperCAmelCase__				(self:				Optional[Any]      )						->    Tuple:




											'''simple docstring'''



											debug_launcher(partial(__UpperCAmelCase   ,				num_processes=1      )   ,				num_processes=1      )
											debug_launcher(partial(__UpperCAmelCase   ,				num_processes=1   ,				split_batches=__UpperCAmelCase      )   ,				num_processes=1      )



						def 					UpperCAmelCase__				(self:				List[str]      )						->    List[Any]:




											'''simple docstring'''



											debug_launcher(partial(__UpperCAmelCase   ,				num_processes=1   ,				step_scheduler_with_optimizer=__UpperCAmelCase      )   ,				num_processes=1      )



						def 					UpperCAmelCase__				(self:				Optional[int]      )						->    List[Any]:




											'''simple docstring'''



											debug_launcher(partial(__UpperCAmelCase   ,				num_processes=1   ,				step_scheduler_with_optimizer=__UpperCAmelCase      )   ,				num_processes=1      )



						def 					UpperCAmelCase__				(self:				Optional[Any]      )						->    Dict:




											'''simple docstring'''



											AcceleratorState._reset_state(__UpperCAmelCase      )
											debug_launcher(__UpperCAmelCase      )
											debug_launcher(partial(__UpperCAmelCase   ,				num_processes=1   ,				split_batches=__UpperCAmelCase      )   ,				num_processes=1      )



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




											'''simple docstring'''



											AcceleratorState._reset_state(__UpperCAmelCase      )
											debug_launcher(__UpperCAmelCase      )
											debug_launcher(partial(__UpperCAmelCase   ,				num_processes=1   ,				split_batches=__UpperCAmelCase      )   ,				num_processes=1      )



						def 					UpperCAmelCase__				(self:				List[Any]      )						->    Optional[int]:




											'''simple docstring'''



											AcceleratorState._reset_state(__UpperCAmelCase      )
											debug_launcher(partial(__UpperCAmelCase   ,				step_scheduler_with_optimizer=__UpperCAmelCase      )      )



						def 					UpperCAmelCase__				(self:				Union[str, Any]      )						->    List[Any]:




											'''simple docstring'''



											AcceleratorState._reset_state(__UpperCAmelCase      )
											debug_launcher(partial(__UpperCAmelCase   ,				step_scheduler_with_optimizer=__UpperCAmelCase      )      )



						@require_huggingface_suite
						def 					UpperCAmelCase__				(self:				List[Any]      )						->    Tuple:




											'''simple docstring'''



											AcceleratorState._reset_state(__UpperCAmelCase      )
											debug_launcher(partial(__UpperCAmelCase   ,				num_processes=1      )      )
											debug_launcher(__UpperCAmelCase      )













def 						a_      (__A							,					__A			)							->	list[tuple[int, int, int]]:



					"""simple docstring"""
					__a				:	str			=				sorted(__A							,					key=lambda __A			: edge[2]			)

					__a				:	int			=				list(range(__A			)			)

					def find_parent(__A			):
										if i != parent[i]:
															__a				:	Optional[Any]			=				find_parent(parent[i]			)
										return parent[i]

					__a				:	Any			=				0
					__a				:	Optional[int]			=				[]

					for edge in edges:
										__a				:	Any			=				find_parent(edge[0]			)
										__a				:	Optional[Any]			=				find_parent(edge[1]			)
										if parent_a != parent_b:
															minimum_spanning_tree_cost += edge[2]
															minimum_spanning_tree.append(__A			)
															__a				:	List[str]			=				parent_b

					return minimum_spanning_tree


if __name__ == "__main__":  # pragma: no cover
			UpperCAmelCase__ ,       UpperCAmelCase__			 =	list(map(int, input().strip().split()))
			UpperCAmelCase__			 =	[]

			for _ in range(num_edges):
						UpperCAmelCase__ ,       UpperCAmelCase__ ,       UpperCAmelCase__			 =	(int(x) for x in input().strip().split())
						edges.append((nodea, nodea, cost))

			kruskal(num_nodes, edges)





