from collections import deque


def SCREAMING_SNAKE_CASE					(  lowercase_:						Optional[Any]					):
       return input(lowercase_					).strip().split(' '					)


def SCREAMING_SNAKE_CASE					(  lowercase_:						int		,    lowercase_:						int					):
       _UpperCamelCase  :							dict[int, list[int]]	    =	{}
       for i in range(lowercase_					):
              _UpperCamelCase  :							List[str]	    =	[]

       for e in range(lowercase_					):
              _UpperCamelCase    ,   _UpperCamelCase  :							Dict	    =	(int(lowercase_					) for i in _input(F"Edge {e + 1}: <node1> <node2> "					))
              graph[x].append(lowercase_					)
       return graph


def SCREAMING_SNAKE_CASE					(  lowercase_:						int		,    lowercase_:						int					):
       _UpperCamelCase  :							dict[int, list[int]]	    =	{}
       for i in range(lowercase_					):
              _UpperCamelCase  :							Optional[Any]	    =	[]

       for e in range(lowercase_					):
              _UpperCamelCase    ,   _UpperCamelCase  :							Tuple	    =	(int(lowercase_					) for i in _input(F"Edge {e + 1}: <node1> <node2> "					))
              graph[x].append(lowercase_					)
              graph[y].append(lowercase_					)
       return graph


def SCREAMING_SNAKE_CASE					(  lowercase_:						int		,    lowercase_:						int					):
       _UpperCamelCase  :							dict[int, list[tuple[int, int]]]	    =	{}
       for i in range(lowercase_					):
              _UpperCamelCase  :							List[Any]	    =	[]

       for e in range(lowercase_					):
              _UpperCamelCase    ,   _UpperCamelCase    ,   _UpperCamelCase  :							Optional[Any]	    =	(int(lowercase_					) for i in _input(F"Edge {e + 1}: <node1> <node2> <weight> "					))
              graph[x].append((y, w)					)
              graph[y].append((x, w)					)
       return graph


if __name__ == "__main__":
 UpperCAmelCase		,       UpperCAmelCase      		=					(int(i) for i in _input('''Number of nodes and edges: '''))

 UpperCAmelCase      		=					int(
     _input(
         '''Press 1 or 2 or 3 \n'''
         '''1. Unweighted directed \n'''
         '''2. Unweighted undirected \n'''
         '''3. Weighted undirected \n'''
     )[0]
 )

 UpperCAmelCase      		=					{
     1: initialize_unweighted_directed_graph,
     2: initialize_unweighted_undirected_graph,
     3: initialize_weighted_undirected_graph,
 }[graph_choice](n, m)


def SCREAMING_SNAKE_CASE					(  lowercase_:						Tuple		,    lowercase_:						Dict					):
       _UpperCamelCase    ,   _UpperCamelCase  :							Dict	    =	{s}, [s]
       print(lowercase_					)
       while _s:
              _UpperCamelCase  :							Union[str, Any]	    =	0
              for i in g[_s[-1]]:
                     if i not in vis:
                            _s.append(lowercase_					)
                            vis.add(lowercase_					)
                            _UpperCamelCase  :							str	    =	1
                            print(lowercase_					)
                            break
              if not flag:
                     _s.pop()


def SCREAMING_SNAKE_CASE					(  lowercase_:						Optional[Any]		,    lowercase_:						str					):
       _UpperCamelCase    ,   _UpperCamelCase  :							Optional[int]	    =	{s}, deque([s]					)
       print(lowercase_					)
       while q:
              _UpperCamelCase  :							int	    =	q.popleft()
              for v in g[u]:
                     if v not in vis:
                            vis.add(lowercase_					)
                            q.append(lowercase_					)
                            print(lowercase_					)


def SCREAMING_SNAKE_CASE					(  lowercase_:						Optional[int]		,    lowercase_:						List[str]					):
       _UpperCamelCase    ,   _UpperCamelCase    ,   _UpperCamelCase  :							str	    =	{s: 0}, set(), {s: 0}
       while True:
              if len(lowercase_					) == len(lowercase_					) - 1:
                     break
              _UpperCamelCase  :							Union[str, Any]	    =	100000
              for i in dist:
                     if i not in known and dist[i] < mini:
                            _UpperCamelCase  :							Any	    =	dist[i]
                            _UpperCamelCase  :							List[Any]	    =	i
              known.add(lowercase_					)
              for v in g[u]:
                     if v[0] not in known and dist[u] + v[1] < dist.get(v[0]		,    100000					):
                            _UpperCamelCase  :							Optional[Any]	    =	dist[u] + v[1]
                            _UpperCamelCase  :							Tuple	    =	u
       for i in dist:
              if i != s:
                     print(dist[i]					)


def SCREAMING_SNAKE_CASE					(  lowercase_:						Optional[int]		,    lowercase_:						Union[str, Any]=None		,    lowercase_:						Union[str, Any]=None					):
       if q is None:
              _UpperCamelCase  :							Any	    =	[1]
       if ind is None:
              _UpperCamelCase  :							Tuple	    =	[0] * (len(lowercase_					) + 1)  # SInce oth Index is ignored
              for u in g:
                     for v in g[u]:
                            ind[v] += 1
              _UpperCamelCase  :							Union[str, Any]	    =	deque()
              for i in g:
                     if ind[i] == 0:
                            q.append(lowercase_					)
       if len(lowercase_					) == 0:
              return
       _UpperCamelCase  :							Optional[Any]	    =	q.popleft()
       print(lowercase_					)
       for w in g[v]:
              ind[w] -= 1
              if ind[w] == 0:
                     q.append(lowercase_					)
       topo(lowercase_		,    lowercase_		,    lowercase_					)


def SCREAMING_SNAKE_CASE					(  ):
       _UpperCamelCase  :							Tuple	    =	input().strip()
       _UpperCamelCase  :							Any	    =	[]
       for _ in range(lowercase_					):
              a.append(map(lowercase_		,    input().strip().split()					)					)
       return a, n


def SCREAMING_SNAKE_CASE					(  lowercase_:						Any					):
       ((_UpperCamelCase)    ,   (_UpperCamelCase))  :							List[Any]	    =	a_and_n
       _UpperCamelCase  :							str	    =	list(lowercase_					)
       _UpperCamelCase  :							Dict	    =	[[0] * n for i in range(lowercase_					)]
       for k in range(lowercase_					):
              for i in range(lowercase_					):
                     for j in range(lowercase_					):
                            if dist[i][j] > dist[i][k] + dist[k][j]:
                                   _UpperCamelCase  :							Any	    =	dist[i][k] + dist[k][j]
                                   _UpperCamelCase  :							Optional[int]	    =	k
       print(lowercase_					)


def SCREAMING_SNAKE_CASE					(  lowercase_:						List[Any]		,    lowercase_:						int					):
       _UpperCamelCase    ,   _UpperCamelCase    ,   _UpperCamelCase  :							int	    =	{s: 0}, set(), {s: 0}
       while True:
              if len(lowercase_					) == len(lowercase_					) - 1:
                     break
              _UpperCamelCase  :							Dict	    =	100000
              for i in dist:
                     if i not in known and dist[i] < mini:
                            _UpperCamelCase  :							Tuple	    =	dist[i]
                            _UpperCamelCase  :							List[Any]	    =	i
              known.add(lowercase_					)
              for v in g[u]:
                     if v[0] not in known and v[1] < dist.get(v[0]		,    100000					):
                            _UpperCamelCase  :							Dict	    =	v[1]
                            _UpperCamelCase  :							Union[str, Any]	    =	u
       return dist


def SCREAMING_SNAKE_CASE					(  ):
       _UpperCamelCase    ,   _UpperCamelCase  :							Any	    =	map(lowercase_		,    input().split(' '					)					)
       _UpperCamelCase  :							List[str]	    =	[]
       for _ in range(lowercase_					):
              edges.append(map(lowercase_		,    input().split(' '					)					)					)
       return edges, n


def SCREAMING_SNAKE_CASE					(  lowercase_:						Tuple					):
       # Sort edges on the basis of distance
       ((_UpperCamelCase)    ,   (_UpperCamelCase))  :							int	    =	e_and_n
       e.sort(reverse=lowercase_		,    key=lambda lowercase_					: x[2]					)
       _UpperCamelCase  :							Dict	    =	[{i} for i in range(1		,    n + 1					)]
       while True:
              if len(lowercase_					) == 1:
                     break
              print(lowercase_					)
              _UpperCamelCase  :							Optional[int]	    =	e.pop()
              for i in range(len(lowercase_					)					):
                     if x[0] in s[i]:
                            break
              for j in range(len(lowercase_					)					):
                     if x[1] in s[j]:
                            if i == j:
                                   break
                            s[j].update(s[i]					)
                            s.pop(lowercase_					)
                            break





def SCREAMING_SNAKE_CASE					(  lowercase_:						List[Any]					):
       _UpperCamelCase  :							Dict	    =	[]
       for node in graph:
              if not graph[node]:
                     isolated.append(lowercase_					)
       return isolated




'''simple docstring'''
import random
from typing import List, Optional, Union

import numpy as np
import torch

from ..state import AcceleratorState
from .constants import CUDA_DISTRIBUTED_TYPES
from .dataclasses import DistributedType, RNGType
from .imports import is_npu_available, is_tpu_available, is_xpu_available


if is_tpu_available(check_device=False):
  import torch_xla.core.xla_model as xm







def 			_UpperCamelCase			(  _a						:						int					,  _a						:						bool = False      ):




    """simple docstring"""



    if device_specific:
        seed += AcceleratorState().process_index
    random.seed(_a      )
    np.random.seed(_a      )
    torch.manual_seed(_a      )
    if is_xpu_available():
        torch.xpu.manual_seed_all(_a      )
    elif is_npu_available():
        torch.npu.manual_seed_all(_a      )
    else:
        torch.cuda.manual_seed_all(_a      )
    # ^^ safe to call this function even if cuda is not available
    if is_tpu_available():
        xm.set_rng_state(_a      )







def 			_UpperCamelCase			(  _a						:						Optional[RNGType] = None					,  _a						:						Optional[torch.Generator] = None      ):




    """simple docstring"""



    if rng_type == RNGType.TORCH:
        __UpperCamelCase						:						Union[str, Any]		     =	torch.get_rng_state()
    elif rng_type == RNGType.CUDA:
        __UpperCamelCase						:						int		     =	torch.cuda.get_rng_state()
    elif rng_type == RNGType.XLA:
        assert is_tpu_available(), "Can't synchronize XLA seeds on an environment without TPUs."
        __UpperCamelCase						:						Dict		     =	torch.tensor(xm.get_rng_state()      )
    elif rng_type == RNGType.NPU:
        assert is_npu_available(), "Can't synchronize NPU seeds on an environment without NPUs."
        __UpperCamelCase						:						Optional[Any]		     =	torch.npu.get_rng_state()
    elif rng_type == RNGType.XPU:
        assert is_xpu_available(), "Can't synchronize XPU seeds on an environment without XPUs."
        __UpperCamelCase						:						str		     =	torch.xpu.get_rng_state()
    elif rng_type == RNGType.GENERATOR:
        assert generator is not None, "Need a generator to synchronize its seed."
        __UpperCamelCase						:						List[Any]		     =	generator.get_state()

    # Broadcast the rng state from device 0 to other devices
    __UpperCamelCase						:						List[Any]		     =	AcceleratorState()
    if state.distributed_type == DistributedType.TPU:
        __UpperCamelCase						:						Union[str, Any]		     =	rng_state.to(xm.xla_device()      )
        xm.collective_broadcast([rng_state]      )
        xm.mark_step()
        __UpperCamelCase						:						str		     =	rng_state.cpu()
    elif (
        state.distributed_type in CUDA_DISTRIBUTED_TYPES
        or state.distributed_type == DistributedType.MULTI_NPU
        or state.distributed_type == DistributedType.MULTI_XPU
    ):
        __UpperCamelCase						:						List[Any]		     =	rng_state.to(state.device      )
        torch.distributed.broadcast(_a					,  0      )
        __UpperCamelCase						:						Optional[Any]		     =	rng_state.cpu()
    elif state.distributed_type == DistributedType.MULTI_CPU:
        torch.distributed.broadcast(_a					,  0      )

    # Set the broadcast rng state
    if rng_type == RNGType.TORCH:
        torch.set_rng_state(_a      )
    elif rng_type == RNGType.CUDA:
        torch.cuda.set_rng_state(_a      )
    elif rng_type == RNGType.NPU:
        torch.npu.set_rng_state(_a      )
    elif rng_type == RNGType.XPU:
        torch.xpu.set_rng_state(_a      )
    elif rng_type == RNGType.XLA:
        xm.set_rng_state(rng_state.item()      )
    elif rng_type == RNGType.GENERATOR:
        generator.set_state(_a      )







def 			_UpperCamelCase			(  _a						:						List[Union[str, RNGType]]					,  _a						:						Optional[torch.Generator] = None      ):




    """simple docstring"""



    for rng_type in rng_types:
        synchronize_rng_state(RNGType(_a      )					,  generator=_a      )



