





'''simple docstring'''







from collections import deque





def 							lowerCamelCase_   (	SCREAMING_SNAKE_CASE__   :      Any )			->						List[Any]:
						return input(SCREAMING_SNAKE_CASE__ ).strip().split(''' ''' )





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

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





def 							lowerCamelCase_   (	SCREAMING_SNAKE_CASE__   :      int,      SCREAMING_SNAKE_CASE__   :      int )			->						dict[int, list[int]]:
						UpperCAmelCase_      :   dict[int, list[int]]      =	{}
						for i in range(SCREAMING_SNAKE_CASE__ ):
												UpperCAmelCase_      :   Any      =	[]

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





def 							lowerCamelCase_   (	SCREAMING_SNAKE_CASE__   :      int,      SCREAMING_SNAKE_CASE__   :      int )			->						dict[int, list[tuple[int, int]]]:
						UpperCAmelCase_      :   dict[int, list[tuple[int, int]]]      =	{}
						for i in range(SCREAMING_SNAKE_CASE__ ):
												UpperCAmelCase_      :   str      =	[]

						for e in range(SCREAMING_SNAKE_CASE__ ):
												UpperCAmelCase_    ,							UpperCAmelCase_    ,							UpperCAmelCase_      :   Tuple      =	(int(SCREAMING_SNAKE_CASE__ ) 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__":
					snake_case_      ,snake_case_  :							Optional[int]            =	(int(i) for i in _input("Number of nodes and edges: "))

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

					snake_case_  :							Union[str, Any]            =	{
					    1: initialize_unweighted_directed_graph,
					    2: initialize_unweighted_undirected_graph,
					    3: initialize_weighted_undirected_graph,
					}[graph_choice](n, m)





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





def 							lowerCamelCase_   (	SCREAMING_SNAKE_CASE__   :      Any,      SCREAMING_SNAKE_CASE__   :      int )			->						Union[str, Any]:
						UpperCAmelCase_    ,							UpperCAmelCase_      :   List[str]      =	{s}, deque([s] )
						print(SCREAMING_SNAKE_CASE__ )
						while q:
												UpperCAmelCase_      :   Dict      =	q.popleft()
												for v in g[u]:
																		if v not in vis:
																								vis.add(SCREAMING_SNAKE_CASE__ )
																								q.append(SCREAMING_SNAKE_CASE__ )
																								print(SCREAMING_SNAKE_CASE__ )





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





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





def 							lowerCamelCase_   (	)			->						List[str]:
						UpperCAmelCase_      :   Union[str, Any]      =	input().strip()
						UpperCAmelCase_      :   int      =	[]
						for _ in range(SCREAMING_SNAKE_CASE__ ):
												a.append(map(SCREAMING_SNAKE_CASE__,      input().strip().split() ) )
						return a, n





def 							lowerCamelCase_   (	SCREAMING_SNAKE_CASE__   :      Union[str, Any] )			->						Tuple:
						((UpperCAmelCase_)    ,							(UpperCAmelCase_))      :   Union[str, Any]      =	a_and_n
						UpperCAmelCase_      :   int      =	list(SCREAMING_SNAKE_CASE__ )
						UpperCAmelCase_      :   Tuple      =	[[0] * n for i in range(SCREAMING_SNAKE_CASE__ )]
						for k in range(SCREAMING_SNAKE_CASE__ ):
												for i in range(SCREAMING_SNAKE_CASE__ ):
																		for j in range(SCREAMING_SNAKE_CASE__ ):
																								if dist[i][j] > dist[i][k] + dist[k][j]:
																														UpperCAmelCase_      :   Tuple      =	dist[i][k] + dist[k][j]
																														UpperCAmelCase_      :   Union[str, Any]      =	k
						print(SCREAMING_SNAKE_CASE__ )





def 							lowerCamelCase_   (	SCREAMING_SNAKE_CASE__   :      Optional[Any],      SCREAMING_SNAKE_CASE__   :      Any )			->						int:
						UpperCAmelCase_    ,							UpperCAmelCase_    ,							UpperCAmelCase_      :   Any      =	{s: 0}, set(), {s: 0}
						while True:
												if len(SCREAMING_SNAKE_CASE__ ) == len(SCREAMING_SNAKE_CASE__ ) - 1:
																		break
												UpperCAmelCase_      :   Optional[Any]      =	100000
												for i in dist:
																		if i not in known and dist[i] < mini:
																								UpperCAmelCase_      :   Optional[Any]      =	dist[i]
																								UpperCAmelCase_      :   Any      =	i
												known.add(SCREAMING_SNAKE_CASE__ )
												for v in g[u]:
																		if v[0] not in known and v[1] < dist.get(v[0],      100000 ):
																								UpperCAmelCase_      :   List[Any]      =	v[1]
																								UpperCAmelCase_      :   str      =	u
						return dist





def 							lowerCamelCase_   (	)			->						int:
						UpperCAmelCase_    ,							UpperCAmelCase_      :   Any      =	map(SCREAMING_SNAKE_CASE__,      input().split(''' ''' ) )
						UpperCAmelCase_      :   List[str]      =	[]
						for _ in range(SCREAMING_SNAKE_CASE__ ):
												edges.append(map(SCREAMING_SNAKE_CASE__,      input().split(''' ''' ) ) )
						return edges, n





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





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









"""simple docstring"""






import os
from typing import List, Union

import tensorflow as tf
from tensorflow_text import BertTokenizer as BertTokenizerLayer
from tensorflow_text import FastBertTokenizer, ShrinkLongestTrimmer, case_fold_utfa, combine_segments, pad_model_inputs

from .tokenization_bert import BertTokenizer


class 	A_       (     tf.keras.layers.Layer   ):





  """simple docstring"""

  def __init__(						self  ,					__UpperCAmelCase  ,					__UpperCAmelCase  ,					__UpperCAmelCase = None  ,					__UpperCAmelCase = None  ,					__UpperCAmelCase = None  ,					__UpperCAmelCase = "longest"  ,					__UpperCAmelCase = True  ,					__UpperCAmelCase = 5_12  ,					__UpperCAmelCase = None  ,					__UpperCAmelCase = True  ,					__UpperCAmelCase = True  ,					__UpperCAmelCase = True  ,					**__UpperCAmelCase  ,					)				->					Union[str, Any]:
     super().__init__()
     if use_fast_bert_tokenizer:
        a   :	int						=    FastBertTokenizer(
            __UpperCAmelCase  ,					token_out_type=tf.intaa  ,					lower_case_nfd_strip_accents=__UpperCAmelCase  ,					**__UpperCAmelCase						)
     else:
        a   :	Optional[Any]						=    tf.lookup.StaticVocabularyTable(
            tf.lookup.KeyValueTensorInitializer(
                keys=__UpperCAmelCase  ,					key_dtype=tf.string  ,					values=tf.range(tf.size(__UpperCAmelCase  ,					out_type=tf.intaa						)  ,					dtype=tf.intaa						)  ,					value_dtype=tf.intaa  ,					)  ,					num_oov_buckets=1  ,					)
        a   :	str						=    BertTokenizerLayer(
            __UpperCAmelCase  ,					token_out_type=tf.intaa  ,					lower_case=__UpperCAmelCase  ,					**__UpperCAmelCase						)

     a   :	str						=    vocab_list
     a   :	Dict						=    do_lower_case
     a   :	Dict						=    cls_token_id or vocab_list.index('[CLS]'						)
     a   :	Tuple						=    sep_token_id or vocab_list.index('[SEP]'						)
     a   :	Optional[Any]						=    pad_token_id or vocab_list.index('[PAD]'						)
     a   :	Tuple						=    ShrinkLongestTrimmer(max_length - 3  ,					axis=1						)  # Allow room for special tokens
     a   :	Union[str, Any]						=    max_length
     a   :	Tuple						=    padding
     a   :	List[Any]						=    truncation
     a   :	Union[str, Any]						=    pad_to_multiple_of
     a   :	List[Any]						=    return_token_type_ids
     a   :	Optional[int]						=    return_attention_mask
  @classmethod
  def 							lowercase_     (						cls  ,					__UpperCAmelCase  ,					**__UpperCAmelCase						)				->					Tuple:  # noqa: F821
     a   :	List[Any]						=    kwargs.pop('do_lower_case'  ,					__UpperCAmelCase						)
     a   :	str						=    tokenizer.do_lower_case if do_lower_case is None else do_lower_case
     a   :	List[Any]						=    kwargs.pop('cls_token_id'  ,					__UpperCAmelCase						)
     a   :	int						=    tokenizer.cls_token_id if cls_token_id is None else cls_token_id
     a   :	Tuple						=    kwargs.pop('sep_token_id'  ,					__UpperCAmelCase						)
     a   :	Optional[int]						=    tokenizer.sep_token_id if sep_token_id is None else sep_token_id
     a   :	List[Any]						=    kwargs.pop('pad_token_id'  ,					__UpperCAmelCase						)
     a   :	Any						=    tokenizer.pad_token_id if pad_token_id is None else pad_token_id

     a   :	Optional[Any]						=    tokenizer.get_vocab()
     a   :	Optional[Any]						=    sorted([(wordpiece, idx) for wordpiece, idx in vocab.items()]  ,					key=lambda __UpperCAmelCase						: x[1]						)
     a   :	Optional[Any]						=    [entry[0] for entry in vocab]
     return cls(
         vocab_list=__UpperCAmelCase  ,					do_lower_case=__UpperCAmelCase  ,					cls_token_id=__UpperCAmelCase  ,					sep_token_id=__UpperCAmelCase  ,					pad_token_id=__UpperCAmelCase  ,					**__UpperCAmelCase  ,					)
  @classmethod
  def 							lowercase_     (						cls  ,					__UpperCAmelCase  ,					*__UpperCAmelCase  ,					**__UpperCAmelCase						)				->					Tuple:
     try:
        a   :	Dict						=    BertTokenizer.from_pretrained(__UpperCAmelCase  ,					*__UpperCAmelCase  ,					**__UpperCAmelCase						)
     except:  # noqa: E722
        from .tokenization_bert_fast import BertTokenizerFast

        a   :	Tuple						=    BertTokenizerFast.from_pretrained(__UpperCAmelCase  ,					*__UpperCAmelCase  ,					**__UpperCAmelCase						)
     return cls.from_tokenizer(__UpperCAmelCase  ,					**__UpperCAmelCase						)
  def 							lowercase_     (						self  ,					__UpperCAmelCase						)				->					List[Any]:
     if self.do_lower_case:
        a   :	int						=    case_fold_utfa(__UpperCAmelCase						)
     a   :	Union[str, Any]						=    self.tf_tokenizer.tokenize(__UpperCAmelCase						)
     return tokens.merge_dims(1  ,					-1						)
  def 							lowercase_     (						self  ,					__UpperCAmelCase  ,					__UpperCAmelCase=None  ,					__UpperCAmelCase=None  ,					__UpperCAmelCase=None  ,					__UpperCAmelCase=None  ,					__UpperCAmelCase=None  ,					__UpperCAmelCase=None  ,					__UpperCAmelCase=None  ,					)				->					Optional[int]:
     if padding is None:
        a   :	List[Any]						=    self.padding
     if padding not in ("longest", "max_length"):
        raise ValueError('Padding must be either \'longest\' or \'max_length\'!'						)
     if max_length is not None and text_pair is not None:
        # Because we have to instantiate a Trimmer to do it properly
        raise ValueError('max_length cannot be overridden at call time when truncating paired texts!'						)
     if max_length is None:
        a   :	Any						=    self.max_length
     if truncation is None:
        a   :	Any						=    self.truncation
     if pad_to_multiple_of is None:
        a   :	Optional[int]						=    self.pad_to_multiple_of
     if return_token_type_ids is None:
        a   :	str						=    self.return_token_type_ids
     if return_attention_mask is None:
        a   :	Dict						=    self.return_attention_mask
     if not isinstance(__UpperCAmelCase  ,					tf.Tensor						):
        a   :	List[str]						=    tf.convert_to_tensor(__UpperCAmelCase						)
     if text_pair is not None and not isinstance(__UpperCAmelCase  ,					tf.Tensor						):
        a   :	Any						=    tf.convert_to_tensor(__UpperCAmelCase						)
     if text_pair is not None:
        if text.shape.rank > 1:
           raise ValueError('text argument should not be multidimensional when a text pair is supplied!'						)
        if text_pair.shape.rank > 1:
           raise ValueError('text_pair should not be multidimensional!'						)
     if text.shape.rank == 2:
        a	, a   :	Union[str, Any]						=    text[:, 0], text[:, 1]
     a   :	Union[str, Any]						=    self.unpaired_tokenize(__UpperCAmelCase						)
     if text_pair is None:  # Unpaired text
        if truncation:
           a   :	List[Any]						=    text[:, : max_length - 2]  # Allow room for special tokens
        a	, a   :	Dict						=    combine_segments(
            (text,)  ,					start_of_sequence_id=self.cls_token_id  ,					end_of_segment_id=self.sep_token_id						)
     else:  # Paired text
        a   :	Optional[Any]						=    self.unpaired_tokenize(__UpperCAmelCase						)
        if truncation:
           a	, a   :	List[str]						=    self.paired_trimmer.trim([text, text_pair]						)
        a	, a   :	List[Any]						=    combine_segments(
            (text, text_pair)  ,					start_of_sequence_id=self.cls_token_id  ,					end_of_segment_id=self.sep_token_id						)
     if padding == "longest":
        a   :	List[Any]						=    input_ids.bounding_shape(axis=1						)
        if pad_to_multiple_of is not None:
           # No ceiling division in tensorflow, so we negate floordiv instead
           a   :	Optional[int]						=    pad_to_multiple_of * (-tf.math.floordiv(-pad_length  ,					__UpperCAmelCase						))
     else:
        a   :	Any						=    max_length

     a	, a   :	Any						=    pad_model_inputs(__UpperCAmelCase  ,					max_seq_length=__UpperCAmelCase  ,					pad_value=self.pad_token_id						)
     a   :	Optional[Any]						=    {'input_ids': input_ids}
     if return_attention_mask:
        a   :	Tuple						=    attention_mask
     if return_token_type_ids:
        a	, a   :	int						=    pad_model_inputs(
            __UpperCAmelCase  ,					max_seq_length=__UpperCAmelCase  ,					pad_value=self.pad_token_id						)
        a   :	Union[str, Any]						=    token_type_ids
     return output






  def 							lowercase_     (						self						)				->					List[str]:
     return {
         "vocab_list": self.vocab_list,
         "do_lower_case": self.do_lower_case,
         "cls_token_id": self.cls_token_id,
         "sep_token_id": self.sep_token_id,
         "pad_token_id": self.pad_token_id,
     }





