




from typing import Union

import torch
from PIL import Image
from torchvision import transforms as tfms
from tqdm.auto import tqdm
from transformers import CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DiffusionPipeline,
    LMSDiscreteScheduler,
    PNDMScheduler,
    UNetaDConditionModel,
)






class 			A			(		_lowerCAmelCase		):
     """simple docstring"""

     def __init__(    self  :   Optional[Any]	,  A__  :   AutoencoderKL	,  A__  :   CLIPTextModel	,  A__  :   CLIPTokenizer	,  A__  :   UNetaDConditionModel	,  A__  :   Union[PNDMScheduler, LMSDiscreteScheduler, DDIMScheduler]	,  )				->       Any:
           super().__init__()

           self.register_modules(vae=A__	,  text_encoder=A__	,  tokenizer=A__	,  unet=A__	,  scheduler=A__			)




     def  __snake_case   (    self  :   str	,  A__  :   List[Any]			)				->       Any:
           with torch.no_grad():
                 lowerCamelCase__        =				self.vae.encode(tfms.ToTensor()(A__			).unsqueeze(0			).to(self.device			) * 2 - 1			)
                 lowerCamelCase__        =				0.18215 * latent.latent_dist.sample()
           return latent




     def  __snake_case   (    self  :   Union[str, Any]	,  A__  :   str			)				->       Tuple:
           lowerCamelCase__        =				(1 / 0.18215) * latent
           with torch.no_grad():
                 lowerCamelCase__        =				self.vae.decode(A__			).sample
           lowerCamelCase__        =				(img / 2 + 0.5).clamp(0	,  1			)
           lowerCamelCase__        =				img.detach().cpu().permute(0	,  2	,  3	,  1			).numpy()
           lowerCamelCase__        =				(img * 2_5_5).round().astype('uint8'			)
           return Image.fromarray(img[0]			)




     def  __snake_case   (    self  :   Dict	,  A__  :   Union[str, Any]			)				->       List[str]:
           lowerCamelCase__        =				self.tokenizer(
               A__	,  padding='max_length'	,  max_length=self.tokenizer.model_max_length	,  truncation=A__	,  return_tensors='pt'	,  )

           lowerCamelCase__        =				self.text_encoder(text_input.input_ids.to(self.device			)			)[0]

           lowerCamelCase__        =				self.tokenizer(
               ''	,  padding='max_length'	,  max_length=self.tokenizer.model_max_length	,  truncation=A__	,  return_tensors='pt'	,  )

           lowerCamelCase__        =				self.text_encoder(uncond_input.input_ids.to(self.device			)			)[0]

           return torch.cat([uncond_embedding, text_embedding]			)




     def __call__(    self  :   str	,  A__  :   Image.Image	,  A__  :   str	,  A__  :   float = 0.3	,  A__  :   float = 0.6	,  A__  :   float = 0.5	,  A__  :   int = 4_2	,  A__  :   int = 5_0	,  A__  :   float = 7.5	,  )				->       Image.Image:
           lowerCamelCase__        =				steps - int(kmin * steps			)
           lowerCamelCase__        =				steps - int(kmax * steps			)

           lowerCamelCase__        =				self.prep_text(A__			)

           self.scheduler.set_timesteps(A__			)

           lowerCamelCase__  ,		lowerCamelCase__        =				img.size
           lowerCamelCase__        =				self.encode(A__			)

           torch.manual_seed(A__			)
           lowerCamelCase__        =				torch.randn(
               (1, self.unet.config.in_channels, height // 8, width // 8)	,  ).to(self.device			)

           lowerCamelCase__        =				self.scheduler.add_noise(
               A__	,  A__	,  timesteps=self.scheduler.timesteps[tmax]	,  )

           lowerCamelCase__        =				torch.cat([latents] * 2			)

           lowerCamelCase__        =				self.scheduler.scale_model_input(A__	,  self.scheduler.timesteps[tmax]			)

           with torch.no_grad():
                 lowerCamelCase__        =				self.unet(
                     A__	,  self.scheduler.timesteps[tmax]	,  encoder_hidden_states=A__	,  ).sample

           lowerCamelCase__  ,		lowerCamelCase__        =				pred.chunk(2			)
           lowerCamelCase__        =				pred_uncond + guidance_scale * (pred_text - pred_uncond)

           lowerCamelCase__        =				self.scheduler.step(A__	,  self.scheduler.timesteps[tmax]	,  A__			).prev_sample

           for i, t in enumerate(tqdm(self.scheduler.timesteps			)			):
                 if i > tmax:
                       if i < tmin:  # layout generation phase
                             lowerCamelCase__        =				self.scheduler.add_noise(
                                 A__	,  A__	,  timesteps=A__	,  )

                             lowerCamelCase__        =				(mix_factor * latents) + (
                                 1 - mix_factor
                             ) * orig_latents  # interpolating between layout noise and conditionally generated noise to preserve layout sematics
                             lowerCamelCase__        =				torch.cat([input] * 2			)

                       else:  # content generation phase
                             lowerCamelCase__        =				torch.cat([latents] * 2			)

                       lowerCamelCase__        =				self.scheduler.scale_model_input(A__	,  A__			)

                       with torch.no_grad():
                             lowerCamelCase__        =				self.unet(
                                 A__	,  A__	,  encoder_hidden_states=A__	,  ).sample

                       lowerCamelCase__  ,		lowerCamelCase__        =				pred.chunk(2			)
                       lowerCamelCase__        =				pred_uncond + guidance_scale * (pred_text - pred_uncond)

                       lowerCamelCase__        =				self.scheduler.step(A__	,  A__	,  A__			).prev_sample

           return self.decode(A__			)





def 			lowerCAmelCase_			(  lowerCamelCase:       int     ,				lowerCamelCase:       list[tuple[int, int, int]]						)					->			list[tuple[int, int, int]]:


      '''simple docstring'''




      lowerCamelCase__        =				sorted(lowerCamelCase     ,				key=lambda lowerCamelCase						: edge[2]						)

      lowerCamelCase__        =				list(range(lowerCamelCase						)						)

      def find_parent(lowerCamelCase:       int						):
            if i != parent[i]:
                  lowerCamelCase__        =				find_parent(parent[i]						)
            return parent[i]

      lowerCamelCase__        =				0
      lowerCamelCase__        =				[]

      for edge in edges:
            lowerCamelCase__        =				find_parent(edge[0]						)
            lowerCamelCase__        =				find_parent(edge[1]						)
            if parent_a != parent_b:
                  minimum_spanning_tree_cost += edge[2]
                  minimum_spanning_tree.append(lowerCamelCase						)
                  lowerCamelCase__        =				parent_b

      return minimum_spanning_tree


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

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

      kruskal(num_nodes, edges)