code: 
  
  
  from collections import deque
  
  
  
  
  def 			__lowercase	(						_UpperCamelCase     )    ->Optional[int]:
  
  
  
  
  
  
  
        """simple docstring"""
  
        return input(_UpperCamelCase     ).strip().split(''' '''     )
  
  
  
  
  def 			__lowercase	(						_UpperCamelCase,    _UpperCamelCase     )    ->dict[int, list[int]]:
  
  
  
  
  
  
  
        """simple docstring"""
  
        lowercase			:   dict[int, list[int]]					   =		{}
        for i in range(_UpperCamelCase     ):
              lowercase			:   Optional[int]					   =		[]
  
        for e in range(_UpperCamelCase     ):
              lowercase   ,			lowercase			:   List[str]					   =		(int(_UpperCamelCase     ) for i in _input(f"""Edge {e + 1}: <node1> <node2> """     ))
              graph[x].append(_UpperCamelCase     )
        return graph
  
  
  
  
  def 			__lowercase	(						_UpperCamelCase,    _UpperCamelCase     )    ->dict[int, list[int]]:
  
  
  
  
  
  
  
        """simple docstring"""
  
        lowercase			:   dict[int, list[int]]					   =		{}
        for i in range(_UpperCamelCase     ):
              lowercase			:   int					   =		[]
  
        for e in range(_UpperCamelCase     ):
              lowercase   ,			lowercase			:   Any					   =		(int(_UpperCamelCase     ) for i in _input(f"""Edge {e + 1}: <node1> <node2> """     ))
              graph[x].append(_UpperCamelCase     )
              graph[y].append(_UpperCamelCase     )
        return graph
  
  
  
  
  def 			__lowercase	(						_UpperCamelCase,    _UpperCamelCase     )    ->dict[int, list[tuple[int, int]]]:
  
  
  
  
  
  
  
        """simple docstring"""
  
        lowercase			:   dict[int, list[tuple[int, int]]]					   =		{}
        for i in range(_UpperCamelCase     ):
              lowercase			:   Dict					   =		[]
  
        for e in range(_UpperCamelCase     ):
              lowercase   ,			lowercase   ,			lowercase			:   List[Any]					   =		(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__":
    __a				,      __a   				=      (int(i) for i in _input('''Number of nodes and edges: '''))
  
    __a   				=      int(
        _input(
            '''Press 1 or 2 or 3 \n'''
            '''1. Unweighted directed \n'''
            '''2. Unweighted undirected \n'''
            '''3. Weighted undirected \n'''
        )[0]
    )
  
    __a   				=      {
        1: initialize_unweighted_directed_graph,
        2: initialize_unweighted_undirected_graph,
        3: initialize_weighted_undirected_graph,
    }[graph_choice](n, m)
  
  
  
  
  def 			__lowercase	(						_UpperCamelCase,    _UpperCamelCase     )    ->Optional[int]:
  
  
  
  
  
  
  
        """simple docstring"""
  
        lowercase   ,			lowercase			:   Tuple					   =		{s}, [s]
        print(_UpperCamelCase     )
        while _s:
              lowercase			:   str					   =		0
              for i in g[_s[-1]]:
                    if i not in vis:
                          _s.append(_UpperCamelCase     )
                          vis.add(_UpperCamelCase     )
                          lowercase			:   Tuple					   =		1
                          print(_UpperCamelCase     )
                          break
              if not flag:
                    _s.pop()
  
  
  
  
  def 			__lowercase	(						_UpperCamelCase,    _UpperCamelCase     )    ->Any:
  
  
  
  
  
  
  
        """simple docstring"""
  
        lowercase   ,			lowercase			:   Any					   =		{s}, deque([s]     )
        print(_UpperCamelCase     )
        while q:
              lowercase			:   Optional[Any]					   =		q.popleft()
              for v in g[u]:
                    if v not in vis:
                          vis.add(_UpperCamelCase     )
                          q.append(_UpperCamelCase     )
                          print(_UpperCamelCase     )
  
  
  
  
  def 			__lowercase	(						_UpperCamelCase,    _UpperCamelCase     )    ->Optional[Any]:
  
  
  
  
  
  
  
        """simple docstring"""
  
        lowercase   ,			lowercase   ,			lowercase			:   Tuple					   =		{s: 0}, set(), {s: 0}
        while True:
              if len(_UpperCamelCase     ) == len(_UpperCamelCase     ) - 1:
                    break
              lowercase			:   Any					   =		100000
              for i in dist:
                    if i not in known and dist[i] < mini:
                          lowercase			:   int					   =		dist[i]
                          lowercase			:   int					   =		i
              known.add(_UpperCamelCase     )
              for v in g[u]:
                    if v[0] not in known and dist[u] + v[1] < dist.get(v[0],    100000     ):
                          lowercase			:   int					   =		dist[u] + v[1]
                          lowercase			:   Union[str, Any]					   =		u
        for i in dist:
              if i != s:
                    print(dist[i]     )
  
  
  
  
  def 			__lowercase	(						_UpperCamelCase,    _UpperCamelCase=None,    _UpperCamelCase=None     )    ->Union[str, Any]:
  
  
  
  
  
  
  
        """simple docstring"""
  
        if q is None:
              lowercase			:   Union[str, Any]					   =		[1]
        if ind is None:
              lowercase			:   Dict					   =		[0] * (len(_UpperCamelCase     ) + 1)  # SInce oth Index is ignored
              for u in g:
                    for v in g[u]:
                          ind[v] += 1
              lowercase			:   int					   =		deque()
              for i in g:
                    if ind[i] == 0:
                          q.append(_UpperCamelCase     )
        if len(_UpperCamelCase     ) == 0:
              return
        lowercase			:   Union[str, Any]					   =		q.popleft()
        print(_UpperCamelCase     )
        for w in g[v]:
              ind[w] -= 1
              if ind[w] == 0:
                    q.append(_UpperCamelCase     )
        topo(_UpperCamelCase,    _UpperCamelCase,    _UpperCamelCase     )
  
  
  
  
  def 			__lowercase	(						)    ->int:
  
  
  
  
  
  
  
        """simple docstring"""
  
        lowercase			:   int					   =		input().strip()
        lowercase			:   int					   =		[]
        for _ in range(_UpperCamelCase     ):
              a.append(map(_UpperCamelCase,    input().strip().split()     )     )
        return a, n
  
  
  
  
  def 			__lowercase	(						_UpperCamelCase     )    ->Union[str, Any]:
  
  
  
  
  
  
  
        """simple docstring"""
  
        ((lowercase)   ,			(lowercase))			:   Any					   =		a_and_n
        lowercase			:   Union[str, Any]					   =		list(_UpperCamelCase     )
        lowercase			:   List[str]					   =		[[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			:   Any					   =		dist[i][k] + dist[k][j]
                                lowercase			:   List[str]					   =		k
        print(_UpperCamelCase     )
  
  
  
  
  def 			__lowercase	(						_UpperCamelCase,    _UpperCamelCase     )    ->Union[str, Any]:
  
  
  
  
  
  
  
        """simple docstring"""
  
        lowercase   ,			lowercase   ,			lowercase			:   List[str]					   =		{s: 0}, set(), {s: 0}
        while True:
              if len(_UpperCamelCase     ) == len(_UpperCamelCase     ) - 1:
                    break
              lowercase			:   str					   =		100000
              for i in dist:
                    if i not in known and dist[i] < mini:
                          lowercase			:   int					   =		dist[i]
                          lowercase			:   int					   =		i
              known.add(_UpperCamelCase     )
              for v in g[u]:
                    if v[0] not in known and v[1] < dist.get(v[0],    100000     ):
                          lowercase			:   str					   =		v[1]
                          lowercase			:   Any					   =		u
        return dist
  
  
  
  
  def 			__lowercase	(						)    ->Tuple:
  
  
  
  
  
  
  
        """simple docstring"""
  
        lowercase   ,			lowercase			:   Optional[int]					   =		map(_UpperCamelCase,    input().split(''' '''     )     )
        lowercase			:   Optional[Any]					   =		[]
        for _ in range(_UpperCamelCase     ):
              edges.append(map(_UpperCamelCase,    input().split(''' '''     )     )     )
        return edges, n
  
  
  
  
  def 			__lowercase	(						_UpperCamelCase     )    ->Optional[Any]:
  
  
  
  
  
  
  
        """simple docstring"""
  
        ((lowercase)   ,			(lowercase))			:   List[Any]					   =		e_and_n
        e.sort(reverse=_UpperCamelCase,    key=lambda _UpperCamelCase     : x[2]     )
        lowercase			:   Any					   =		[{i} for i in range(1,    n + 1     )]
        while True:
              if len(_UpperCamelCase     ) == 1:
                    break
              print(_UpperCamelCase     )
              lowercase			:   Dict					   =		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 			__lowercase	(						_UpperCamelCase     )    ->Union[str, Any]:
  
  
  
  
  
  
  
        """simple docstring"""
  
        lowercase			:   Optional[Any]					   =		[]
        for node in graph:
              if not graph[node]:
                    isolated.append(_UpperCamelCase     )
        return isolated
  
  
style_context: 
  
  
  from __future__ import annotations
  
  
  
  
  def 			__lowercase	(						_UpperCamelCase     )    ->list[int]:
  
  
  
  
  
  
  
        """simple docstring"""
  
        assert (
            isinstance(_UpperCamelCase,    _UpperCamelCase     ) and mask > 0
        ), f"""mask needs to be positive integer, your input {mask}"""
        lowercase			:   Optional[int]					   =		[]
        lowercase			:   Optional[int]					   =		mask
  
        while submask:
              all_submasks.append(_UpperCamelCase     )
              lowercase			:   Dict					   =		(submask - 1) & mask
  
        return all_submasks
  
  
  if __name__ == "__main__":
    import doctest
  
    doctest.testmod()
  
  