





from collections import deque


def 			lowerCamelCase_		(					lowerCAmelCase:		Union[str, Any] )->						Optional[int]:
  return input(lowerCAmelCase ).strip().split(' ' )


def 			lowerCamelCase_		(					lowerCAmelCase:		int			,							lowerCAmelCase:		int )->						dict[int, list[int]]:
  _snake_case      : dict[int, list[int]]														=		{}
  for i in range(lowerCAmelCase ):
    _snake_case      : Tuple														=		[]

  for e in range(lowerCAmelCase ):
    _snake_case							,    _snake_case      : Union[str, Any]														=		(int(lowerCAmelCase ) for i in _input(F"""Edge {e + 1}: <node1> <node2> """ ))
    graph[x].append(lowerCAmelCase )
  return graph


def 			lowerCamelCase_		(					lowerCAmelCase:		int			,							lowerCAmelCase:		int )->						dict[int, list[int]]:
  _snake_case      : dict[int, list[int]]														=		{}
  for i in range(lowerCAmelCase ):
    _snake_case      : Optional[int]														=		[]

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


def 			lowerCamelCase_		(					lowerCAmelCase:		int			,							lowerCAmelCase:		int )->						dict[int, list[tuple[int, int]]]:
  _snake_case      : dict[int, list[tuple[int, int]]]														=		{}
  for i in range(lowerCAmelCase ):
    _snake_case      : List[Any]														=		[]

  for e in range(lowerCAmelCase ):
    _snake_case							,    _snake_case							,    _snake_case      : List[str]														=		(int(lowerCAmelCase ) 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__":
      lowerCAmelCase_      ,							lowerCAmelCase_        =			(int(i) for i in _input("""Number of nodes and edges: """))

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

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


def 			lowerCamelCase_		(					lowerCAmelCase:		Dict			,							lowerCAmelCase:		List[Any] )->						List[str]:
  _snake_case							,    _snake_case      : Any														=		{s}, [s]
  print(lowerCAmelCase )
  while _s:
    _snake_case      : Any														=		0
    for i in g[_s[-1]]:
      if i not in vis:
        _s.append(lowerCAmelCase )
        vis.add(lowerCAmelCase )
        _snake_case      : str														=		1
        print(lowerCAmelCase )
        break
    if not flag:
      _s.pop()


def 			lowerCamelCase_		(					lowerCAmelCase:		str			,							lowerCAmelCase:		Any )->						Tuple:
  _snake_case							,    _snake_case      : Any														=		{s}, deque([s] )
  print(lowerCAmelCase )
  while q:
    _snake_case      : Any														=		q.popleft()
    for v in g[u]:
      if v not in vis:
        vis.add(lowerCAmelCase )
        q.append(lowerCAmelCase )
        print(lowerCAmelCase )


def 			lowerCamelCase_		(					lowerCAmelCase:		List[Any]			,							lowerCAmelCase:		Optional[Any] )->						Dict:
  _snake_case							,    _snake_case							,    _snake_case      : Optional[Any]														=		{s: 0}, set(), {s: 0}
  while True:
    if len(lowerCAmelCase ) == len(lowerCAmelCase ) - 1:
      break
    _snake_case      : Dict														=		10_00_00
    for i in dist:
      if i not in known and dist[i] < mini:
        _snake_case      : int														=		dist[i]
        _snake_case      : Optional[int]														=		i
    known.add(lowerCAmelCase )
    for v in g[u]:
      if v[0] not in known and dist[u] + v[1] < dist.get(v[0]			,							10_00_00 ):
        _snake_case      : Dict														=		dist[u] + v[1]
        _snake_case      : Tuple														=		u
  for i in dist:
    if i != s:
      print(dist[i] )


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


def 			lowerCamelCase_		(					)->						str:
  _snake_case      : Dict														=		input().strip()
  _snake_case      : Optional[Any]														=		[]
  for _ in range(lowerCAmelCase ):
    a.append(map(lowerCAmelCase			,							input().strip().split() ) )
  return a, n


def 			lowerCamelCase_		(					lowerCAmelCase:		Optional[Any] )->						Dict:
  ((_snake_case)							,    (_snake_case))      : Dict														=		a_and_n
  _snake_case      : Union[str, Any]														=		list(lowerCAmelCase )
  _snake_case      : Optional[Any]														=		[[0] * n for i in range(lowerCAmelCase )]
  for k in range(lowerCAmelCase ):
    for i in range(lowerCAmelCase ):
      for j in range(lowerCAmelCase ):
        if dist[i][j] > dist[i][k] + dist[k][j]:
          _snake_case      : List[Any]														=		dist[i][k] + dist[k][j]
          _snake_case      : Optional[int]														=		k
  print(lowerCAmelCase )


def 			lowerCamelCase_		(					lowerCAmelCase:		List[str]			,							lowerCAmelCase:		Any )->						List[str]:
  _snake_case							,    _snake_case							,    _snake_case      : Union[str, Any]														=		{s: 0}, set(), {s: 0}
  while True:
    if len(lowerCAmelCase ) == len(lowerCAmelCase ) - 1:
      break
    _snake_case      : Dict														=		10_00_00
    for i in dist:
      if i not in known and dist[i] < mini:
        _snake_case      : str														=		dist[i]
        _snake_case      : Dict														=		i
    known.add(lowerCAmelCase )
    for v in g[u]:
      if v[0] not in known and v[1] < dist.get(v[0]			,							10_00_00 ):
        _snake_case      : List[Any]														=		v[1]
        _snake_case      : Tuple														=		u
  return dist


def 			lowerCamelCase_		(					)->						Union[str, Any]:
  _snake_case							,    _snake_case      : Union[str, Any]														=		map(lowerCAmelCase			,							input().split(' ' ) )
  _snake_case      : Optional[Any]														=		[]
  for _ in range(lowerCAmelCase ):
    edges.append(map(lowerCAmelCase			,							input().split(' ' ) ) )
  return edges, n


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







def 			lowerCamelCase_		(					lowerCAmelCase:		str )->						List[Any]:
  _snake_case      : Optional[Any]														=		[]
  for node in graph:
    if not graph[node]:
      isolated.append(lowerCAmelCase )
  return isolated






"""simple docstring"""

import torch

from diffusers import KDPMaAncestralDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest






class        _lowercase   (				__UpperCAmelCase		):
	lowercase_        =	(KDPMaAncestralDiscreteScheduler,)
	lowercase_        =	1_0




	def 			_UpperCamelCase    ( self		, **UpperCAmelCase_					)		->							Optional[int]:
			lowerCamelCase     :			Any		=    {
			    'num_train_timesteps': 1100,
			    'beta_start': 0.0001,
			    'beta_end': 0.02,
			    'beta_schedule': 'linear',
			}

			config.update(**UpperCAmelCase_					)
			return config




	def 			_UpperCamelCase    ( self					)		->							Any:
			for timesteps in [10, 50, 100, 1000]:
					self.check_over_configs(num_train_timesteps=UpperCAmelCase_					)




	def 			_UpperCamelCase    ( self					)		->							List[str]:
			for beta_start, beta_end in zip([0.00001, 0.0001, 0.001]		, [0.0002, 0.002, 0.02]					):
					self.check_over_configs(beta_start=UpperCAmelCase_		, beta_end=UpperCAmelCase_					)




	def 			_UpperCamelCase    ( self					)		->							Optional[int]:
			for schedule in ["linear", "scaled_linear"]:
					self.check_over_configs(beta_schedule=UpperCAmelCase_					)




	def 			_UpperCamelCase    ( self					)		->							List[Any]:
			if torch_device == "mps":
					return
			lowerCamelCase     :			Any		=    self.scheduler_classes[0]
			lowerCamelCase     :			List[str]		=    self.get_scheduler_config()
			lowerCamelCase     :			Union[str, Any]		=    scheduler_class(**UpperCAmelCase_					)

			scheduler.set_timesteps(self.num_inference_steps					)

			lowerCamelCase     :			Optional[Any]		=    torch.manual_seed(0					)

			lowerCamelCase     :			int		=    self.dummy_model()
			lowerCamelCase     :			Tuple		=    self.dummy_sample_deter * scheduler.init_noise_sigma
			lowerCamelCase     :			Union[str, Any]		=    sample.to(UpperCAmelCase_					)

			for i, t in enumerate(scheduler.timesteps					):
					lowerCamelCase     :			Tuple		=    scheduler.scale_model_input(UpperCAmelCase_		, UpperCAmelCase_					)

					lowerCamelCase     :			List[str]		=    model(UpperCAmelCase_		, UpperCAmelCase_					)

					lowerCamelCase     :			Tuple		=    scheduler.step(UpperCAmelCase_		, UpperCAmelCase_		, UpperCAmelCase_		, generator=UpperCAmelCase_					)
					lowerCamelCase     :			str		=    output.prev_sample

			lowerCamelCase     :			Optional[int]		=    torch.sum(torch.abs(UpperCAmelCase_					)					)
			lowerCamelCase     :			Tuple		=    torch.mean(torch.abs(UpperCAmelCase_					)					)

			assert abs(result_sum.item() - 13849.3877					) < 1E-2
			assert abs(result_mean.item() - 18.0331					) < 5E-3




	def 			_UpperCamelCase    ( self					)		->							Optional[int]:
			for prediction_type in ["epsilon", "v_prediction"]:
					self.check_over_configs(prediction_type=UpperCAmelCase_					)




	def 			_UpperCamelCase    ( self					)		->							List[str]:
			if torch_device == "mps":
					return
			lowerCamelCase     :			Dict		=    self.scheduler_classes[0]
			lowerCamelCase     :			Union[str, Any]		=    self.get_scheduler_config(prediction_type='v_prediction'					)
			lowerCamelCase     :			int		=    scheduler_class(**UpperCAmelCase_					)

			scheduler.set_timesteps(self.num_inference_steps					)

			lowerCamelCase     :			int		=    self.dummy_model()
			lowerCamelCase     :			Dict		=    self.dummy_sample_deter * scheduler.init_noise_sigma
			lowerCamelCase     :			Optional[int]		=    sample.to(UpperCAmelCase_					)

			lowerCamelCase     :			Dict		=    torch.manual_seed(0					)

			for i, t in enumerate(scheduler.timesteps					):
					lowerCamelCase     :			Dict		=    scheduler.scale_model_input(UpperCAmelCase_		, UpperCAmelCase_					)

					lowerCamelCase     :			Optional[Any]		=    model(UpperCAmelCase_		, UpperCAmelCase_					)

					lowerCamelCase     :			Tuple		=    scheduler.step(UpperCAmelCase_		, UpperCAmelCase_		, UpperCAmelCase_		, generator=UpperCAmelCase_					)
					lowerCamelCase     :			Optional[Any]		=    output.prev_sample

			lowerCamelCase     :			Union[str, Any]		=    torch.sum(torch.abs(UpperCAmelCase_					)					)
			lowerCamelCase     :			Optional[Any]		=    torch.mean(torch.abs(UpperCAmelCase_					)					)

			assert abs(result_sum.item() - 328.9970					) < 1E-2
			assert abs(result_mean.item() - 0.4284					) < 1E-3




	def 			_UpperCamelCase    ( self					)		->							Any:
			if torch_device == "mps":
					return
			lowerCamelCase     :			Union[str, Any]		=    self.scheduler_classes[0]
			lowerCamelCase     :			Dict		=    self.get_scheduler_config()
			lowerCamelCase     :			Dict		=    scheduler_class(**UpperCAmelCase_					)

			scheduler.set_timesteps(self.num_inference_steps		, device=UpperCAmelCase_					)
			lowerCamelCase     :			int		=    torch.manual_seed(0					)

			lowerCamelCase     :			List[Any]		=    self.dummy_model()
			lowerCamelCase     :			Union[str, Any]		=    self.dummy_sample_deter.to(UpperCAmelCase_					) * scheduler.init_noise_sigma

			for t in scheduler.timesteps:
					lowerCamelCase     :			Optional[int]		=    scheduler.scale_model_input(UpperCAmelCase_		, UpperCAmelCase_					)

					lowerCamelCase     :			str		=    model(UpperCAmelCase_		, UpperCAmelCase_					)

					lowerCamelCase     :			List[str]		=    scheduler.step(UpperCAmelCase_		, UpperCAmelCase_		, UpperCAmelCase_		, generator=UpperCAmelCase_					)
					lowerCamelCase     :			Dict		=    output.prev_sample

			lowerCamelCase     :			List[str]		=    torch.sum(torch.abs(UpperCAmelCase_					)					)
			lowerCamelCase     :			str		=    torch.mean(torch.abs(UpperCAmelCase_					)					)

			assert abs(result_sum.item() - 13849.3818					) < 1E-1
			assert abs(result_mean.item() - 18.0331					) < 1E-3






