


from collections import deque






def 		a__       (							A__							):
			return input(A__							).strip().split(' '							)






def 		a__       (							A__,	A__							):
			SCREAMING_SNAKE_CASE_						:			dict[int, list[int]]       						= {}
			for i in range(A__							):
						SCREAMING_SNAKE_CASE_						:			List[str]       						= []

			for e in range(A__							):
						SCREAMING_SNAKE_CASE_						,	SCREAMING_SNAKE_CASE_						:			Optional[int]       						= (int(A__							) for i in _input(F'''Edge {e + 1}: <node1> <node2> '''							))
						graph[x].append(A__							)
			return graph






def 		a__       (							A__,	A__							):
			SCREAMING_SNAKE_CASE_						:			dict[int, list[int]]       						= {}
			for i in range(A__							):
						SCREAMING_SNAKE_CASE_						:			Optional[Any]       						= []

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






def 		a__       (							A__,	A__							):
			SCREAMING_SNAKE_CASE_						:			dict[int, list[tuple[int, int]]]       						= {}
			for i in range(A__							):
						SCREAMING_SNAKE_CASE_						:			Tuple       						= []

			for e in range(A__							):
						SCREAMING_SNAKE_CASE_						,	SCREAMING_SNAKE_CASE_						,	SCREAMING_SNAKE_CASE_						:			Union[str, Any]       						= (int(A__							) 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__       :	List[Any]     				=(int(i) for i in _input('Number of nodes and edges: '))

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

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






def 		a__       (							A__,	A__							):
			SCREAMING_SNAKE_CASE_						,	SCREAMING_SNAKE_CASE_						:			List[Any]       						= {s}, [s]
			print(A__							)
			while _s:
						SCREAMING_SNAKE_CASE_						:			Optional[int]       						= 0
						for i in g[_s[-1]]:
									if i not in vis:
												_s.append(A__							)
												vis.add(A__							)
												SCREAMING_SNAKE_CASE_						:			Optional[Any]       						= 1
												print(A__							)
												break
						if not flag:
									_s.pop()






def 		a__       (							A__,	A__							):
			SCREAMING_SNAKE_CASE_						,	SCREAMING_SNAKE_CASE_						:			Dict       						= {s}, deque([s]							)
			print(A__							)
			while q:
						SCREAMING_SNAKE_CASE_						:			Any       						= q.popleft()
						for v in g[u]:
									if v not in vis:
												vis.add(A__							)
												q.append(A__							)
												print(A__							)






def 		a__       (							A__,	A__							):
			SCREAMING_SNAKE_CASE_						,	SCREAMING_SNAKE_CASE_						,	SCREAMING_SNAKE_CASE_						:			Optional[Any]       						= {s: 0}, set(), {s: 0}
			while True:
						if len(A__							) == len(A__							) - 1:
									break
						SCREAMING_SNAKE_CASE_						:			Any       						= 1_0_0_0_0_0
						for i in dist:
									if i not in known and dist[i] < mini:
												SCREAMING_SNAKE_CASE_						:			List[str]       						= dist[i]
												SCREAMING_SNAKE_CASE_						:			str       						= i
						known.add(A__							)
						for v in g[u]:
									if v[0] not in known and dist[u] + v[1] < dist.get(v[0],	1_0_0_0_0_0							):
												SCREAMING_SNAKE_CASE_						:			List[Any]       						= dist[u] + v[1]
												SCREAMING_SNAKE_CASE_						:			List[Any]       						= u
			for i in dist:
						if i != s:
									print(dist[i]							)






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






def 		a__       (							):
			SCREAMING_SNAKE_CASE_						:			Any       						= input().strip()
			SCREAMING_SNAKE_CASE_						:			List[Any]       						= []
			for _ in range(A__							):
						a.append(map(A__,	input().strip().split()							)							)
			return a, n






def 		a__       (							A__							):
			((SCREAMING_SNAKE_CASE_)						,	(SCREAMING_SNAKE_CASE_))						:			Tuple       						= a_and_n
			SCREAMING_SNAKE_CASE_						:			int       						= list(A__							)
			SCREAMING_SNAKE_CASE_						:			List[str]       						= [[0] * n for i in range(A__							)]
			for k in range(A__							):
						for i in range(A__							):
									for j in range(A__							):
												if dist[i][j] > dist[i][k] + dist[k][j]:
															SCREAMING_SNAKE_CASE_						:			List[str]       						= dist[i][k] + dist[k][j]
															SCREAMING_SNAKE_CASE_						:			Optional[int]       						= k
			print(A__							)






def 		a__       (							A__,	A__							):
			SCREAMING_SNAKE_CASE_						,	SCREAMING_SNAKE_CASE_						,	SCREAMING_SNAKE_CASE_						:			Any       						= {s: 0}, set(), {s: 0}
			while True:
						if len(A__							) == len(A__							) - 1:
									break
						SCREAMING_SNAKE_CASE_						:			Union[str, Any]       						= 1_0_0_0_0_0
						for i in dist:
									if i not in known and dist[i] < mini:
												SCREAMING_SNAKE_CASE_						:			Optional[Any]       						= dist[i]
												SCREAMING_SNAKE_CASE_						:			Dict       						= i
						known.add(A__							)
						for v in g[u]:
									if v[0] not in known and v[1] < dist.get(v[0],	1_0_0_0_0_0							):
												SCREAMING_SNAKE_CASE_						:			int       						= v[1]
												SCREAMING_SNAKE_CASE_						:			Union[str, Any]       						= u
			return dist






def 		a__       (							):
			SCREAMING_SNAKE_CASE_						,	SCREAMING_SNAKE_CASE_						:			int       						= map(A__,	input().split(' '							)							)
			SCREAMING_SNAKE_CASE_						:			Dict       						= []
			for _ in range(A__							):
						edges.append(map(A__,	input().split(' '							)							)							)
			return edges, n






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






def 		a__       (							A__							):
			SCREAMING_SNAKE_CASE_						:			Optional[int]       						= []
			for node in graph:
						if not graph[node]:
									isolated.append(A__							)
			return isolated











from __future__ import annotations

from typing import Generic, TypeVar

UpperCamelCase								=     TypeVar('T')





class  __lowerCamelCase       (    Generic[T]    ):




    """simple docstring"""

    def __init__(       self     :     List[str]   ,	SCREAMING_SNAKE_CASE__     :     T   )      ->       List[Any]:
         lowerCAmelCase__		= data  # Assign data
         lowerCAmelCase__		= None  # Initialize next as null
         lowerCAmelCase__		= None  # Initialize prev as null






class  __lowerCamelCase       (    Generic[T]    ):




    """simple docstring"""

    def __init__(       self     :     Any   )      ->       None:
         lowerCAmelCase__		= None


    def    a      (       self     :     Tuple   ,	SCREAMING_SNAKE_CASE__     :     T   )      ->       None:
         if self.head is None:
              lowerCAmelCase__		= Node(SCREAMING_SNAKE_CASE__   )
         else:
              lowerCAmelCase__		= Node(SCREAMING_SNAKE_CASE__   )
              lowerCAmelCase__		= new_node
              lowerCAmelCase__		= self.head
              lowerCAmelCase__		= None
              lowerCAmelCase__		= new_node


    def    a      (       self     :     int   )      ->       T | None:
         if self.head is None:
              return None
         else:
              assert self.head is not None
              lowerCAmelCase__		= self.head.data
              lowerCAmelCase__		= self.head.next
              if self.head is not None:
                   lowerCAmelCase__		= None
              return temp


    def    a      (       self     :     int   )      ->       T | None:
         return self.head.data if self.head is not None else None


    def __len__(       self     :     List[str]   )      ->       int:
         lowerCAmelCase__		= self.head
         lowerCAmelCase__		= 0
         while temp is not None:
              count += 1
              lowerCAmelCase__		= temp.next
         return count


    def    a      (       self     :     Tuple   )      ->       bool:
         return self.head is None


    def    a      (       self     :     Dict   )      ->       None:
         print("stack elements are:"   )
         lowerCAmelCase__		= self.head
         while temp is not None:
              print(temp.data   ,	end="->"   )
              lowerCAmelCase__		= temp.next


# Code execution starts here
if __name__ == "__main__":
     # Start with the empty stack
     UpperCamelCase								=     Stack()

     # Insert 4 at the beginning. So stack becomes 4->None
     print('Stack operations using Doubly LinkedList')
     stack.push(4)

     # Insert 5 at the beginning. So stack becomes 4->5->None
     stack.push(5)

     # Insert 6 at the beginning. So stack becomes 4->5->6->None
     stack.push(6)

     # Insert 7 at the beginning. So stack becomes 4->5->6->7->None
     stack.push(7)

     # Print the stack
     stack.print_stack()

     # Print the top element
     print('\nTop element is ', stack.top())

     # Print the stack size
     print('Size of the stack is ', len(stack))

     # pop the top element
     stack.pop()

     # pop the top element
     stack.pop()

     # two elements have now been popped off
     stack.print_stack()

     # Print True if the stack is empty else False
     print('\nstack is empty:', stack.is_empty())
