

"""simple docstring"""






from collections import deque
def 		A_	(   __UpperCamelCase     :				Union[str, Any]				):
		return input(UpperCamelCase_				).strip().split(''' '''				)
def 		A_	(   __UpperCamelCase     :				int       ,	__UpperCamelCase     :				int				):
		lowercase										=							{}
		for i in range(UpperCamelCase_				):
				lowercase										=							[]

		for e in range(UpperCamelCase_				):
				lowercase					,  lowercase										=							(int(UpperCamelCase_				) for i in _input(f"""Edge {e + 1}: <node1> <node2> """				))
				graph[x].append(UpperCamelCase_				)
		return graph
def 		A_	(   __UpperCamelCase     :				int       ,	__UpperCamelCase     :				int				):
		lowercase										=							{}
		for i in range(UpperCamelCase_				):
				lowercase										=							[]

		for e in range(UpperCamelCase_				):
				lowercase					,  lowercase										=							(int(UpperCamelCase_				) for i in _input(f"""Edge {e + 1}: <node1> <node2> """				))
				graph[x].append(UpperCamelCase_				)
				graph[y].append(UpperCamelCase_				)
		return graph
def 		A_	(   __UpperCamelCase     :				int       ,	__UpperCamelCase     :				int				):
		lowercase										=							{}
		for i in range(UpperCamelCase_				):
				lowercase										=							[]

		for e in range(UpperCamelCase_				):
				lowercase					,  lowercase					,  lowercase										=							(int(UpperCamelCase_				) 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				      =   (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 		A_	(   __UpperCamelCase     :				Optional[int]       ,	__UpperCamelCase     :				Dict				):
		lowercase					,  lowercase										=							{s}, [s]
		print(UpperCamelCase_				)
		while _s:
				lowercase										=							0
				for i in g[_s[-1]]:
						if i not in vis:
								_s.append(UpperCamelCase_				)
								vis.add(UpperCamelCase_				)
								lowercase										=							1
								print(UpperCamelCase_				)
								break
				if not flag:
						_s.pop()
def 		A_	(   __UpperCamelCase     :				Any       ,	__UpperCamelCase     :				List[Any]				):
		lowercase					,  lowercase										=							{s}, deque([s]				)
		print(UpperCamelCase_				)
		while q:
				lowercase										=							q.popleft()
				for v in g[u]:
						if v not in vis:
								vis.add(UpperCamelCase_				)
								q.append(UpperCamelCase_				)
								print(UpperCamelCase_				)
def 		A_	(   __UpperCamelCase     :				Optional[Any]       ,	__UpperCamelCase     :				Union[str, Any]				):
		lowercase					,  lowercase					,  lowercase										=							{s: 0}, set(), {s: 0}
		while True:
				if len(UpperCamelCase_				) == len(UpperCamelCase_				) - 1:
						break
				lowercase										=							10_00_00
				for i in dist:
						if i not in known and dist[i] < mini:
								lowercase										=							dist[i]
								lowercase										=							i
				known.add(UpperCamelCase_				)
				for v in g[u]:
						if v[0] not in known and dist[u] + v[1] < dist.get(v[0]       ,	10_00_00				):
								lowercase										=							dist[u] + v[1]
								lowercase										=							u
		for i in dist:
				if i != s:
						print(dist[i]				)
def 		A_	(   __UpperCamelCase     :				Union[str, Any]       ,	__UpperCamelCase     :				Any=None       ,	__UpperCamelCase     :				Dict=None				):



		if q is None:
				lowercase										=							[1]
		if ind is None:
				lowercase										=							[0] * (len(UpperCamelCase_				) + 1)  # SInce oth Index is ignored
				for u in g:
						for v in g[u]:
								ind[v] += 1
				lowercase										=							deque()
				for i in g:
						if ind[i] == 0:
								q.append(UpperCamelCase_				)
		if len(UpperCamelCase_				) == 0:
				return
		lowercase										=							q.popleft()
		print(UpperCamelCase_				)
		for w in g[v]:
				ind[w] -= 1
				if ind[w] == 0:
						q.append(UpperCamelCase_				)
		topo(UpperCamelCase_       ,	UpperCamelCase_       ,	UpperCamelCase_				)
def 		A_	(   ):
		lowercase										=							input().strip()
		lowercase										=							[]
		for _ in range(UpperCamelCase_				):
				a.append(map(UpperCamelCase_       ,	input().strip().split()				)				)
		return a, n
def 		A_	(   __UpperCamelCase     :				int				):
		((lowercase)					,  (lowercase))										=							a_and_n
		lowercase										=							list(UpperCamelCase_				)
		lowercase										=							[[0] * n for i in range(UpperCamelCase_				)]
		for k in range(UpperCamelCase_				):
				for i in range(UpperCamelCase_				):
						for j in range(UpperCamelCase_				):
								if dist[i][j] > dist[i][k] + dist[k][j]:
										lowercase										=							dist[i][k] + dist[k][j]
										lowercase										=							k
		print(UpperCamelCase_				)
def 		A_	(   __UpperCamelCase     :				int       ,	__UpperCamelCase     :				Optional[Any]				):
		lowercase					,  lowercase					,  lowercase										=							{s: 0}, set(), {s: 0}
		while True:
				if len(UpperCamelCase_				) == len(UpperCamelCase_				) - 1:
						break
				lowercase										=							10_00_00
				for i in dist:
						if i not in known and dist[i] < mini:
								lowercase										=							dist[i]
								lowercase										=							i
				known.add(UpperCamelCase_				)
				for v in g[u]:
						if v[0] not in known and v[1] < dist.get(v[0]       ,	10_00_00				):
								lowercase										=							v[1]
								lowercase										=							u
		return dist
def 		A_	(   ):
		lowercase					,  lowercase										=							map(UpperCamelCase_       ,	input().split(''' '''				)				)
		lowercase										=							[]
		for _ in range(UpperCamelCase_				):
				edges.append(map(UpperCamelCase_       ,	input().split(''' '''				)				)				)
		return edges, n
def 		A_	(   __UpperCamelCase     :				List[str]				):
		((lowercase)					,  (lowercase))										=							e_and_n
		e.sort(reverse=UpperCamelCase_       ,	key=lambda __UpperCamelCase				: x[2]				)
		lowercase										=							[{i} for i in range(1       ,	n + 1				)]
		while True:
				if len(UpperCamelCase_				) == 1:
						break
				print(UpperCamelCase_				)
				lowercase										=							e.pop()
				for i in range(len(UpperCamelCase_				)				):
						if x[0] in s[i]:
								break
				for j in range(len(UpperCamelCase_				)				):
						if x[1] in s[j]:
								if i == j:
										break
								s[j].update(s[i]				)
								s.pop(UpperCamelCase_				)
								break



def 		A_	(   __UpperCamelCase     :				Optional[int]				):
		lowercase										=							[]
		for node in graph:
				if not graph[node]:
						isolated.append(UpperCamelCase_				)
		return isolated







from __future__ import annotations

from collections.abc import Iterable, Iterator
from typing import Any, Generic, TypeVar

__snake_case     :     str       =  TypeVar("""T""", bound=bool)


class      __SCREAMING_SNAKE_CASE       ( Generic[T]):






						def __init__(						self  ,    _UpperCamelCase ):






												"""simple docstring"""
												lowerCAmelCase__         =    value
												lowerCAmelCase__         =    None
												lowerCAmelCase__         =    None






						@property
						def    UpperCamelCase__   (						self ):






												"""simple docstring"""
												return self._value







						@staticmethod
						def    UpperCamelCase__   (						_UpperCamelCase  ,    _UpperCamelCase ):






												"""simple docstring"""
												if not roota:
																		return roota

												if not roota:
																		return roota

												if roota.value > roota.value:
																		lowerCAmelCase__    ,							lowerCAmelCase__         =    roota, roota

												lowerCAmelCase__         =    roota
												lowerCAmelCase__         =    roota.right
												lowerCAmelCase__         =    roota.left
												lowerCAmelCase__         =    SkewNode.merge(_UpperCamelCase  ,    _UpperCamelCase )

												return result




class      __SCREAMING_SNAKE_CASE       ( Generic[T]):






						def __init__(						self  ,    _UpperCamelCase = () ):






												"""simple docstring"""
												lowerCAmelCase__         =    None
												if data:
																		for item in data:
																								self.insert(_UpperCamelCase )






						def __bool__(						self ):






												"""simple docstring"""
												return self._root is not None






						def __iter__(						self ):






												"""simple docstring"""
												lowerCAmelCase__         =    []
												while self:
																		result.append(self.pop() )

												# Pushing items back to the heap not to clear it.
												for item in result:
																		self.insert(_UpperCamelCase )

												return iter(_UpperCamelCase )






						def    UpperCamelCase__   (						self  ,    _UpperCamelCase ):






												"""simple docstring"""
												lowerCAmelCase__         =    SkewNode.merge(self._root  ,    SkewNode(_UpperCamelCase ) )






						def    UpperCamelCase__   (						self ):






												"""simple docstring"""
												lowerCAmelCase__         =    self.top()
												lowerCAmelCase__         =    (
												    SkewNode.merge(self._root.left  ,    self._root.right ) if self._root else None
												)

												return result






						def    UpperCamelCase__   (						self ):






												"""simple docstring"""
												if not self._root:
																		raise IndexError('Can\'t get top element for the empty heap.' )
												return self._root.value







						def    UpperCamelCase__   (						self ):






												"""simple docstring"""
												lowerCAmelCase__         =    None


if __name__ == "__main__":
					import doctest

					doctest.testmod()





