




"""simple docstring"""

import os
import tempfile
import unittest

from transformers import NezhaConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch

  from transformers import (
      MODEL_FOR_PRETRAINING_MAPPING,
      NezhaForMaskedLM,
      NezhaForMultipleChoice,
      NezhaForNextSentencePrediction,
      NezhaForPreTraining,
      NezhaForQuestionAnswering,
      NezhaForSequenceClassification,
      NezhaForTokenClassification,
      NezhaModel,
  )
  from transformers.models.nezha.modeling_nezha import NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST

class        lowercase      :







    def __init__(							self   :					Union[str, Any]      ,     _lowerCamelCase   :					List[str]      ,     _lowerCamelCase   :					int=13      ,     _lowerCamelCase   :					Optional[int]=7      ,     _lowerCamelCase   :					Optional[int]=True      ,     _lowerCamelCase   :					Union[str, Any]=True      ,     _lowerCamelCase   :					Dict=True      ,     _lowerCamelCase   :					int=True      ,     _lowerCamelCase   :					str=99      ,     _lowerCamelCase   :					Any=32      ,     _lowerCamelCase   :					Tuple=5      ,     _lowerCamelCase   :					Any=4      ,     _lowerCamelCase   :					List[str]=37      ,     _lowerCamelCase   :					str="gelu"      ,     _lowerCamelCase   :					Optional[Any]=0.1      ,     _lowerCamelCase   :					Optional[Any]=0.1      ,     _lowerCamelCase   :					Tuple=1_28      ,     _lowerCamelCase   :					Optional[Any]=32      ,     _lowerCamelCase   :					str=16      ,     _lowerCamelCase   :					Optional[int]=2      ,     _lowerCamelCase   :					Any=0.02      ,     _lowerCamelCase   :					Optional[Any]=3      ,     _lowerCamelCase   :					str=4      ,     _lowerCamelCase   :					str=None      ,     ):



     """simple docstring"""

     A_  :				List[Any]	   =		parent
     A_  :				Union[str, Any]	   =		batch_size
     A_  :				List[Any]	   =		seq_length
     A_  :				Optional[Any]	   =		is_training
     A_  :				str	   =		use_input_mask
     A_  :				Any	   =		use_token_type_ids
     A_  :				Tuple	   =		use_labels
     A_  :				int	   =		vocab_size
     A_  :				int	   =		hidden_size
     A_  :				List[Any]	   =		num_hidden_layers
     A_  :				Optional[int]	   =		num_attention_heads
     A_  :				Tuple	   =		intermediate_size
     A_  :				Union[str, Any]	   =		hidden_act
     A_  :				Tuple	   =		hidden_dropout_prob
     A_  :				Tuple	   =		attention_probs_dropout_prob
     A_  :				Dict	   =		max_position_embeddings
     A_  :				List[Any]	   =		type_vocab_size
     A_  :				List[str]	   =		type_sequence_label_size
     A_  :				Optional[Any]	   =		initializer_range
     A_  :				Union[str, Any]	   =		num_labels
     A_  :				Union[str, Any]	   =		num_choices
     A_  :				Union[str, Any]	   =		scope







    def 					a_  (							self   :					Tuple       ):



     """simple docstring"""

     A_  :				List[str]	   =		ids_tensor([self.batch_size, self.seq_length]      ,     self.vocab_size       )

     A_  :				str	   =		None
     if self.use_input_mask:
      A_  :				int	   =		random_attention_mask([self.batch_size, self.seq_length]       )

     A_  :				Optional[int]	   =		None
     if self.use_token_type_ids:
      A_  :				Tuple	   =		ids_tensor([self.batch_size, self.seq_length]      ,     self.type_vocab_size       )

     A_  :				str	   =		None
     A_  :				Optional[int]	   =		None
     A_  :				Any	   =		None
     if self.use_labels:
      A_  :				Any	   =		ids_tensor([self.batch_size]      ,     self.type_sequence_label_size       )
      A_  :				Dict	   =		ids_tensor([self.batch_size, self.seq_length]      ,     self.num_labels       )
      A_  :				str	   =		ids_tensor([self.batch_size]      ,     self.num_choices       )

     A_  :				int	   =		self.get_config()

     return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels







    def 					a_  (							self   :					Optional[int]       ):



     """simple docstring"""

     return NezhaConfig(
         vocab_size=self.vocab_size      ,     hidden_size=self.hidden_size      ,     num_hidden_layers=self.num_hidden_layers      ,     num_attention_heads=self.num_attention_heads      ,     intermediate_size=self.intermediate_size      ,     hidden_act=self.hidden_act      ,     hidden_dropout_prob=self.hidden_dropout_prob      ,     attention_probs_dropout_prob=self.attention_probs_dropout_prob      ,     max_position_embeddings=self.max_position_embeddings      ,     type_vocab_size=self.type_vocab_size      ,     is_decoder=_a      ,     initializer_range=self.initializer_range      ,     )







    def 					a_  (							self   :					Dict       ):



     """simple docstring"""

     (
         A_		
     )  :				List[Any]	   =		self.prepare_config_and_inputs()

     A_  :				Tuple	   =		True
     A_  :				Optional[Any]	   =		floats_tensor([self.batch_size, self.seq_length, self.hidden_size]       )
     A_  :				Union[str, Any]	   =		ids_tensor([self.batch_size, self.seq_length]      ,     vocab_size=2       )

     return (
         config,
         input_ids,
         token_type_ids,
         input_mask,
         sequence_labels,
         token_labels,
         choice_labels,
         encoder_hidden_states,
         encoder_attention_mask,
     )







    def 					a_  (							self   :					str      ,     _lowerCamelCase   :					Optional[int]      ,     _lowerCamelCase   :					Optional[int]      ,     _lowerCamelCase   :					Optional[int]      ,     _lowerCamelCase   :					Dict      ,     _lowerCamelCase   :					List[Any]      ,     _lowerCamelCase   :					int      ,     _lowerCamelCase   :					Tuple       ):



     """simple docstring"""

     A_  :				List[str]	   =		NezhaModel(config=_a       )
     model.to(_a       )
     model.eval()
     A_  :				int	   =		model(_a      ,     attention_mask=_a      ,     token_type_ids=_a       )
     A_  :				List[str]	   =		model(_a      ,     token_type_ids=_a       )
     A_  :				List[str]	   =		model(_a       )
     self.parent.assertEqual(result.last_hidden_state.shape      ,     (self.batch_size, self.seq_length, self.hidden_size)       )
     self.parent.assertEqual(result.pooler_output.shape      ,     (self.batch_size, self.hidden_size)       )







    def 					a_  (							self   :					Union[str, Any]      ,     _lowerCamelCase   :					Union[str, Any]      ,     _lowerCamelCase   :					Dict      ,     _lowerCamelCase   :					Union[str, Any]      ,     _lowerCamelCase   :					int      ,     _lowerCamelCase   :					Tuple      ,     _lowerCamelCase   :					Optional[Any]      ,     _lowerCamelCase   :					List[Any]      ,     _lowerCamelCase   :					Optional[int]      ,     _lowerCamelCase   :					Union[str, Any]      ,     ):



     """simple docstring"""

     A_  :				List[str]	   =		True
     A_  :				List[str]	   =		NezhaModel(_a       )
     model.to(_a       )
     model.eval()
     A_  :				Tuple	   =		model(
         _a      ,     attention_mask=_a      ,     token_type_ids=_a      ,     encoder_hidden_states=_a      ,     encoder_attention_mask=_a      ,     )
     A_  :				List[str]	   =		model(
         _a      ,     attention_mask=_a      ,     token_type_ids=_a      ,     encoder_hidden_states=_a      ,     )
     A_  :				str	   =		model(_a      ,     attention_mask=_a      ,     token_type_ids=_a       )
     self.parent.assertEqual(result.last_hidden_state.shape      ,     (self.batch_size, self.seq_length, self.hidden_size)       )
     self.parent.assertEqual(result.pooler_output.shape      ,     (self.batch_size, self.hidden_size)       )







    def 					a_  (							self   :					Optional[Any]      ,     _lowerCamelCase   :					Optional[Any]      ,     _lowerCamelCase   :					Union[str, Any]      ,     _lowerCamelCase   :					Union[str, Any]      ,     _lowerCamelCase   :					Optional[Any]      ,     _lowerCamelCase   :					List[Any]      ,     _lowerCamelCase   :					Tuple      ,     _lowerCamelCase   :					Optional[int]       ):



     """simple docstring"""

     A_  :				Optional[Any]	   =		NezhaForMaskedLM(config=_a       )
     model.to(_a       )
     model.eval()
     A_  :				Any	   =		model(_a      ,     attention_mask=_a      ,     token_type_ids=_a      ,     labels=_a       )
     self.parent.assertEqual(result.logits.shape      ,     (self.batch_size, self.seq_length, self.vocab_size)       )







    def 					a_  (							self   :					int      ,     _lowerCamelCase   :					Optional[Any]      ,     _lowerCamelCase   :					int      ,     _lowerCamelCase   :					Any      ,     _lowerCamelCase   :					Tuple      ,     _lowerCamelCase   :					Optional[Any]      ,     _lowerCamelCase   :					List[Any]      ,     _lowerCamelCase   :					Tuple       ):



     """simple docstring"""

     A_  :				str	   =		NezhaForNextSentencePrediction(config=_a       )
     model.to(_a       )
     model.eval()
     A_  :				Tuple	   =		model(
         _a      ,     attention_mask=_a      ,     token_type_ids=_a      ,     labels=_a      ,     )
     self.parent.assertEqual(result.logits.shape      ,     (self.batch_size, 2)       )







    def 					a_  (							self   :					Dict      ,     _lowerCamelCase   :					Dict      ,     _lowerCamelCase   :					Tuple      ,     _lowerCamelCase   :					str      ,     _lowerCamelCase   :					Union[str, Any]      ,     _lowerCamelCase   :					Any      ,     _lowerCamelCase   :					Dict      ,     _lowerCamelCase   :					Dict       ):



     """simple docstring"""

     A_  :				List[Any]	   =		NezhaForPreTraining(config=_a       )
     model.to(_a       )
     model.eval()
     A_  :				Union[str, Any]	   =		model(
         _a      ,     attention_mask=_a      ,     token_type_ids=_a      ,     labels=_a      ,     next_sentence_label=_a      ,     )
     self.parent.assertEqual(result.prediction_logits.shape      ,     (self.batch_size, self.seq_length, self.vocab_size)       )
     self.parent.assertEqual(result.seq_relationship_logits.shape      ,     (self.batch_size, 2)       )







    def 					a_  (							self   :					Dict      ,     _lowerCamelCase   :					List[Any]      ,     _lowerCamelCase   :					List[str]      ,     _lowerCamelCase   :					List[Any]      ,     _lowerCamelCase   :					int      ,     _lowerCamelCase   :					str      ,     _lowerCamelCase   :					str      ,     _lowerCamelCase   :					List[Any]       ):



     """simple docstring"""

     A_  :				Any	   =		NezhaForQuestionAnswering(config=_a       )
     model.to(_a       )
     model.eval()
     A_  :				Optional[int]	   =		model(
         _a      ,     attention_mask=_a      ,     token_type_ids=_a      ,     start_positions=_a      ,     end_positions=_a      ,     )
     self.parent.assertEqual(result.start_logits.shape      ,     (self.batch_size, self.seq_length)       )
     self.parent.assertEqual(result.end_logits.shape      ,     (self.batch_size, self.seq_length)       )







    def 					a_  (							self   :					List[Any]      ,     _lowerCamelCase   :					List[str]      ,     _lowerCamelCase   :					Any      ,     _lowerCamelCase   :					Dict      ,     _lowerCamelCase   :					List[Any]      ,     _lowerCamelCase   :					Union[str, Any]      ,     _lowerCamelCase   :					List[str]      ,     _lowerCamelCase   :					Tuple       ):



     """simple docstring"""

     A_  :				Optional[Any]	   =		self.num_labels
     A_  :				int	   =		NezhaForSequenceClassification(_a       )
     model.to(_a       )
     model.eval()
     A_  :				Optional[int]	   =		model(_a      ,     attention_mask=_a      ,     token_type_ids=_a      ,     labels=_a       )
     self.parent.assertEqual(result.logits.shape      ,     (self.batch_size, self.num_labels)       )







    def 					a_  (							self   :					Optional[int]      ,     _lowerCamelCase   :					Dict      ,     _lowerCamelCase   :					str      ,     _lowerCamelCase   :					Optional[Any]      ,     _lowerCamelCase   :					List[Any]      ,     _lowerCamelCase   :					List[str]      ,     _lowerCamelCase   :					List[str]      ,     _lowerCamelCase   :					Optional[int]       ):



     """simple docstring"""

     A_  :				str	   =		self.num_labels
     A_  :				Dict	   =		NezhaForTokenClassification(config=_a       )
     model.to(_a       )
     model.eval()
     A_  :				Dict	   =		model(_a      ,     attention_mask=_a      ,     token_type_ids=_a      ,     labels=_a       )
     self.parent.assertEqual(result.logits.shape      ,     (self.batch_size, self.seq_length, self.num_labels)       )







    def 					a_  (							self   :					List[Any]      ,     _lowerCamelCase   :					List[str]      ,     _lowerCamelCase   :					Tuple      ,     _lowerCamelCase   :					Tuple      ,     _lowerCamelCase   :					List[Any]      ,     _lowerCamelCase   :					Optional[int]      ,     _lowerCamelCase   :					List[Any]      ,     _lowerCamelCase   :					Dict       ):



     """simple docstring"""

     A_  :				Any	   =		self.num_choices
     A_  :				str	   =		NezhaForMultipleChoice(config=_a       )
     model.to(_a       )
     model.eval()
     A_  :				List[Any]	   =		input_ids.unsqueeze(1       ).expand(-1      ,     self.num_choices      ,     -1       ).contiguous()
     A_  :				Optional[Any]	   =		token_type_ids.unsqueeze(1       ).expand(-1      ,     self.num_choices      ,     -1       ).contiguous()
     A_  :				str	   =		input_mask.unsqueeze(1       ).expand(-1      ,     self.num_choices      ,     -1       ).contiguous()
     A_  :				Any	   =		model(
         _a      ,     attention_mask=_a      ,     token_type_ids=_a      ,     labels=_a      ,     )
     self.parent.assertEqual(result.logits.shape      ,     (self.batch_size, self.num_choices)       )







    def 					a_  (							self   :					Optional[Any]       ):



     """simple docstring"""

     A_  :				int	   =		self.prepare_config_and_inputs()
     (
         A_		
     )  :				str	   =		config_and_inputs
     A_  :				Union[str, Any]	   =		{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
     return config, inputs_dict

@require_torch
class        lowercase      (    UpperCamelCase_     ,	UpperCamelCase_     ,	UpperCamelCase_     ,	unittest.TestCase):
    __lowerCAmelCase    :    Union[str, Any]         =   (
        (
            NezhaModel,
            NezhaForMaskedLM,
            NezhaForMultipleChoice,
            NezhaForNextSentencePrediction,
            NezhaForPreTraining,
            NezhaForQuestionAnswering,
            NezhaForSequenceClassification,
            NezhaForTokenClassification,
        )
        if is_torch_available()
        else ()
    )
    __lowerCAmelCase    :    List[Any]         =   (
        {
            """feature-extraction""": NezhaModel,
            """fill-mask""": NezhaForMaskedLM,
            """question-answering""": NezhaForQuestionAnswering,
            """text-classification""": NezhaForSequenceClassification,
            """token-classification""": NezhaForTokenClassification,
            """zero-shot""": NezhaForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    __lowerCAmelCase    :    Optional[Any]         =   True







    def 					a_  (							self   :					Tuple      ,     _lowerCamelCase   :					Union[str, Any]      ,     _lowerCamelCase   :					Dict      ,     _lowerCamelCase   :					int=False       ):



     """simple docstring"""

     A_  :				Optional[int]	   =		super()._prepare_for_class(_a      ,     _a      ,     return_labels=_a       )

     if return_labels:
      if model_class in get_values(_a       ):
       A_  :				Tuple	   =		torch.zeros(
           (self.model_tester.batch_size, self.model_tester.seq_length)      ,     dtype=torch.long      ,     device=_a       )
       A_  :				Tuple	   =		torch.zeros(
           self.model_tester.batch_size      ,     dtype=torch.long      ,     device=_a       )
     return inputs_dict







    def 					a_  (							self   :					str       ):



     """simple docstring"""

     A_  :				Tuple	   =		NezhaModelTester(self       )
     A_  :				int	   =		ConfigTester(self      ,     config_class=_a      ,     hidden_size=37       )







    def 					a_  (							self   :					List[str]       ):



     """simple docstring"""

     self.config_tester.run_common_tests()







    def 					a_  (							self   :					int       ):



     """simple docstring"""

     A_  :				List[Any]	   =		self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*_a       )







    def 					a_  (							self   :					Optional[Any]       ):



     """simple docstring"""

     A_  :				Dict	   =		self.model_tester.prepare_config_and_inputs_for_decoder()
     self.model_tester.create_and_check_model_as_decoder(*_a       )







    def 					a_  (							self   :					List[Any]       ):



     """simple docstring"""

     (
         A_		
     )  :				Optional[int]	   =		self.model_tester.prepare_config_and_inputs_for_decoder()

     A_  :				Any	   =		None

     self.model_tester.create_and_check_model_as_decoder(
         _a      ,     _a      ,     _a      ,     _a      ,     _a      ,     _a      ,     _a      ,     _a      ,     _a      ,     )







    def 					a_  (							self   :					Any       ):



     """simple docstring"""

     A_  :				str	   =		self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_lm(*_a       )







    def 					a_  (							self   :					Optional[int]       ):



     """simple docstring"""

     A_  :				List[str]	   =		self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_multiple_choice(*_a       )







    def 					a_  (							self   :					str       ):



     """simple docstring"""

     A_  :				int	   =		self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_next_sequence_prediction(*_a       )







    def 					a_  (							self   :					List[Any]       ):



     """simple docstring"""

     A_  :				List[Any]	   =		self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_pretraining(*_a       )







    def 					a_  (							self   :					Union[str, Any]       ):



     """simple docstring"""

     A_  :				Optional[Any]	   =		self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*_a       )







    def 					a_  (							self   :					Dict       ):



     """simple docstring"""

     A_  :				Any	   =		self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*_a       )







    def 					a_  (							self   :					Optional[int]       ):



     """simple docstring"""

     A_  :				int	   =		self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*_a       )







    @slow
    def 					a_  (							self   :					Dict       ):



     """simple docstring"""

     for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
      A_  :				Optional[Any]	   =		NezhaModel.from_pretrained(_a       )
      self.assertIsNotNone(_a       )







    @slow
    @require_torch_gpu
    def 					a_  (							self   :					Any       ):



     """simple docstring"""

     A_  :				str	   =		self.model_tester.prepare_config_and_inputs_for_common()
     for model_class in self.all_model_classes:
      # NezhaForMultipleChoice behaves incorrectly in JIT environments.
      if model_class == NezhaForMultipleChoice:
       return

      A_  :				Optional[int]	   =		True
      A_  :				Optional[Any]	   =		model_class(config=_a       )

      A_  :				Any	   =		self._prepare_for_class(_a      ,     _a       )
      A_  :				Union[str, Any]	   =		torch.jit.trace(
          _a      ,     (inputs_dict['''input_ids'''].to('''cpu'''       ), inputs_dict['''attention_mask'''].to('''cpu'''       ))       )

      with tempfile.TemporaryDirectory() as tmp:
       torch.jit.save(_a      ,     os.path.join(_a      ,     '''bert.pt'''       )       )
       A_  :				Any	   =		torch.jit.load(os.path.join(_a      ,     '''bert.pt'''       )      ,     map_location=_a       )
       loaded(inputs_dict['''input_ids'''].to(_a       )      ,     inputs_dict['''attention_mask'''].to(_a       )       )

@require_torch
class        lowercase      (    unittest.TestCase):







    @slow
    def 					a_  (							self   :					Union[str, Any]       ):



     """simple docstring"""

     A_  :				str	   =		NezhaModel.from_pretrained('''sijunhe/nezha-cn-base'''       )
     A_  :				Union[str, Any]	   =		torch.tensor([[0, 1, 2, 3, 4, 5]]       )
     A_  :				str	   =		torch.tensor([[0, 1, 1, 1, 1, 1]]       )
     with torch.no_grad():
      A_  :				Dict	   =		model(_a      ,     attention_mask=_a       )[0]
     A_  :				str	   =		torch.Size((1, 6, 7_68)       )
     self.assertEqual(output.shape      ,     _a       )
     A_  :				int	   =		torch.tensor([[[0.0685, 0.2441, 0.1102], [0.0600, 0.1906, 0.1349], [0.0221, 0.0819, 0.0586]]]       )

     self.assertTrue(torch.allclose(output[:, 1:4, 1:4]      ,     _a      ,     atol=1E-4       )       )







    @slow
    def 					a_  (							self   :					List[Any]       ):



     """simple docstring"""

     A_  :				List[str]	   =		NezhaForMaskedLM.from_pretrained('''sijunhe/nezha-cn-base'''       )
     A_  :				Union[str, Any]	   =		torch.tensor([[0, 1, 2, 3, 4, 5]]       )
     A_  :				List[str]	   =		torch.tensor([[1, 1, 1, 1, 1, 1]]       )
     with torch.no_grad():
      A_  :				Dict	   =		model(_a      ,     attention_mask=_a       )[0]
     A_  :				List[str]	   =		torch.Size((1, 6, 2_11_28)       )
     self.assertEqual(output.shape      ,     _a       )
     A_  :				Union[str, Any]	   =		torch.tensor(
         [[-2.7939, -1.7902, -2.2189], [-2.8585, -1.8908, -2.3723], [-2.6499, -1.7750, -2.2558]]       )

     self.assertTrue(torch.allclose(output[:, 1:4, 1:4]      ,     _a      ,     atol=1E-4       )       )











"""simple docstring"""

from collections import deque
from math import floor
from random import random
from time import time

class        lowercase      :







				def __init__(							self   :					Dict       ):



					"""simple docstring"""

					A_  :				Tuple	   =		{}







				def 					a_  (							self   :					Union[str, Any]      ,     _lowerCamelCase   :					List[Any]      ,     _lowerCamelCase   :					Dict      ,     _lowerCamelCase   :					List[Any]=1       ):



					"""simple docstring"""

					if self.graph.get(_lowerCamelCase       ):
						if self.graph[u].count([w, v]       ) == 0:
							self.graph[u].append([w, v]       )
					else:
						A_  :				int	   =		[[w, v]]
					if not self.graph.get(_lowerCamelCase       ):
						A_  :				List[Any]	   =		[]







				def 					a_  (							self   :					Optional[int]       ):



					"""simple docstring"""

					return list(self.graph       )







				def 					a_  (							self   :					Dict      ,     _lowerCamelCase   :					str      ,     _lowerCamelCase   :					Tuple       ):



					"""simple docstring"""

					if self.graph.get(_lowerCamelCase       ):
						for _ in self.graph[u]:
							if _[1] == v:
								self.graph[u].remove(_lowerCamelCase       )







				def 					a_  (							self   :					Union[str, Any]      ,     _lowerCamelCase   :					Optional[int]=-2      ,     _lowerCamelCase   :					Tuple=-1       ):



					"""simple docstring"""

					if s == d:
						return []
					A_  :				Union[str, Any]	   =		[]
					A_  :				Optional[int]	   =		[]
					if s == -2:
						A_  :				int	   =		list(self.graph       )[0]
					stack.append(_lowerCamelCase       )
					visited.append(_lowerCamelCase       )
					A_  :				Tuple	   =		s

					while True:
						# check if there is any non isolated nodes
						if len(self.graph[s]       ) != 0:
							A_  :				Optional[Any]	   =		s
							for node in self.graph[s]:
								if visited.count(node[1]       ) < 1:
									if node[1] == d:
										visited.append(_lowerCamelCase       )
										return visited
									else:
										stack.append(node[1]       )
										visited.append(node[1]       )
										A_  :				Union[str, Any]	   =		node[1]
										break

            # check if all the children are visited
						if s == ss:
							stack.pop()
							if len(_lowerCamelCase       ) != 0:
								A_  :				Optional[int]	   =		stack[len(_lowerCamelCase       ) - 1]
						else:
							A_  :				List[Any]	   =		ss

						# check if se have reached the starting point
						if len(_lowerCamelCase       ) == 0:
							return visited







				def 					a_  (							self   :					Dict      ,     _lowerCamelCase   :					List[Any]=-1       ):



					"""simple docstring"""

					if c == -1:
						A_  :				List[Any]	   =		floor(random() * 1_00_00       ) + 10
					for i in range(_lowerCamelCase       ):
						# every vertex has max 100 edges
						for _ in range(floor(random() * 1_02       ) + 1       ):
							A_  :				Tuple	   =		floor(random() * c       ) + 1
							if n != i:
								self.add_pair(_lowerCamelCase      ,     _lowerCamelCase      ,     1       )







				def 					a_  (							self   :					Optional[int]      ,     _lowerCamelCase   :					Union[str, Any]=-2       ):



					"""simple docstring"""

					A_  :				List[str]	   =		deque()
					A_  :				int	   =		[]
					if s == -2:
						A_  :				Any	   =		list(self.graph       )[0]
					d.append(_lowerCamelCase       )
					visited.append(_lowerCamelCase       )
					while d:
						A_  :				List[Any]	   =		d.popleft()
						if len(self.graph[s]       ) != 0:
							for node in self.graph[s]:
								if visited.count(node[1]       ) < 1:
									d.append(node[1]       )
									visited.append(node[1]       )
					return visited







				def 					a_  (							self   :					Union[str, Any]      ,     _lowerCamelCase   :					Tuple       ):



					"""simple docstring"""

					A_  :				List[Any]	   =		0
					for x in self.graph:
						for y in self.graph[x]:
							if y[1] == u:
								count += 1
					return count







				def 					a_  (							self   :					Any      ,     _lowerCamelCase   :					Optional[Any]       ):



					"""simple docstring"""

					return len(self.graph[u]       )







				def 					a_  (							self   :					Union[str, Any]      ,     _lowerCamelCase   :					Tuple=-2       ):



					"""simple docstring"""

					A_  :				int	   =		[]
					A_  :				Optional[Any]	   =		[]
					if s == -2:
						A_  :				List[str]	   =		list(self.graph       )[0]
					stack.append(_lowerCamelCase       )
					visited.append(_lowerCamelCase       )
					A_  :				str	   =		s
					A_  :				List[Any]	   =		[]

					while True:
						# check if there is any non isolated nodes
						if len(self.graph[s]       ) != 0:
							A_  :				str	   =		s
							for node in self.graph[s]:
								if visited.count(node[1]       ) < 1:
									stack.append(node[1]       )
									visited.append(node[1]       )
									A_  :				List[Any]	   =		node[1]
									break

            # check if all the children are visited
						if s == ss:
							sorted_nodes.append(stack.pop()       )
							if len(_lowerCamelCase       ) != 0:
								A_  :				List[str]	   =		stack[len(_lowerCamelCase       ) - 1]
						else:
							A_  :				int	   =		ss

						# check if se have reached the starting point
						if len(_lowerCamelCase       ) == 0:
							return sorted_nodes







				def 					a_  (							self   :					Dict       ):



					"""simple docstring"""

					A_  :				str	   =		[]
					A_  :				Dict	   =		[]
					A_  :				Dict	   =		list(self.graph       )[0]
					stack.append(_lowerCamelCase       )
					visited.append(_lowerCamelCase       )
					A_  :				Any	   =		-2
					A_  :				List[str]	   =		[]
					A_  :				Dict	   =		s
					A_  :				Tuple	   =		False
					A_  :				List[str]	   =		set()

					while True:
						# check if there is any non isolated nodes
						if len(self.graph[s]       ) != 0:
							A_  :				Dict	   =		s
							for node in self.graph[s]:
								if (
								    visited.count(node[1]       ) > 0
								    and node[1] != parent
								    and indirect_parents.count(node[1]       ) > 0
								    and not on_the_way_back
								):
									A_  :				Tuple	   =		len(_lowerCamelCase       ) - 1
									while len_stack >= 0:
										if stack[len_stack] == node[1]:
											anticipating_nodes.add(node[1]       )
											break
										else:
											anticipating_nodes.add(stack[len_stack]       )
											len_stack -= 1
								if visited.count(node[1]       ) < 1:
									stack.append(node[1]       )
									visited.append(node[1]       )
									A_  :				int	   =		node[1]
									break

            # check if all the children are visited
						if s == ss:
							stack.pop()
							A_  :				Optional[int]	   =		True
							if len(_lowerCamelCase       ) != 0:
								A_  :				Union[str, Any]	   =		stack[len(_lowerCamelCase       ) - 1]
						else:
							A_  :				int	   =		False
							indirect_parents.append(_lowerCamelCase       )
							A_  :				Dict	   =		s
							A_  :				Optional[int]	   =		ss

						# check if se have reached the starting point
						if len(_lowerCamelCase       ) == 0:
							return list(_lowerCamelCase       )







				def 					a_  (							self   :					List[str]       ):



					"""simple docstring"""

					A_  :				Dict	   =		[]
					A_  :				Dict	   =		[]
					A_  :				Optional[Any]	   =		list(self.graph       )[0]
					stack.append(_lowerCamelCase       )
					visited.append(_lowerCamelCase       )
					A_  :				Optional[Any]	   =		-2
					A_  :				List[str]	   =		[]
					A_  :				List[Any]	   =		s
					A_  :				int	   =		False
					A_  :				Any	   =		set()

					while True:
						# check if there is any non isolated nodes
						if len(self.graph[s]       ) != 0:
							A_  :				Union[str, Any]	   =		s
							for node in self.graph[s]:
								if (
								    visited.count(node[1]       ) > 0
								    and node[1] != parent
								    and indirect_parents.count(node[1]       ) > 0
								    and not on_the_way_back
								):
									A_  :				Dict	   =		len(_lowerCamelCase       ) - 1
									while len_stack_minus_one >= 0:
										if stack[len_stack_minus_one] == node[1]:
											anticipating_nodes.add(node[1]       )
											break
										else:
											return True
								if visited.count(node[1]       ) < 1:
									stack.append(node[1]       )
									visited.append(node[1]       )
									A_  :				Any	   =		node[1]
									break

            # check if all the children are visited
						if s == ss:
							stack.pop()
							A_  :				str	   =		True
							if len(_lowerCamelCase       ) != 0:
								A_  :				Optional[int]	   =		stack[len(_lowerCamelCase       ) - 1]
						else:
							A_  :				Tuple	   =		False
							indirect_parents.append(_lowerCamelCase       )
							A_  :				int	   =		s
							A_  :				List[Any]	   =		ss

						# check if se have reached the starting point
						if len(_lowerCamelCase       ) == 0:
							return False







				def 					a_  (							self   :					Tuple      ,     _lowerCamelCase   :					Union[str, Any]=-2      ,     _lowerCamelCase   :					int=-1       ):



					"""simple docstring"""

					A_  :				int	   =		time()
					self.dfs(_lowerCamelCase      ,     _lowerCamelCase       )
					A_  :				int	   =		time()
					return end - begin







				def 					a_  (							self   :					Optional[Any]      ,     _lowerCamelCase   :					str=-2       ):



					"""simple docstring"""

					A_  :				int	   =		time()
					self.bfs(_lowerCamelCase       )
					A_  :				Union[str, Any]	   =		time()
					return end - begin

class        lowercase      :







				def __init__(							self   :					Any       ):



					"""simple docstring"""

					A_  :				Tuple	   =		{}







				def 					a_  (							self   :					List[Any]      ,     _lowerCamelCase   :					Optional[Any]      ,     _lowerCamelCase   :					Optional[Any]      ,     _lowerCamelCase   :					Optional[int]=1       ):



					"""simple docstring"""

					if self.graph.get(_lowerCamelCase       ):
						# if there already is a edge
						if self.graph[u].count([w, v]       ) == 0:
							self.graph[u].append([w, v]       )
					else:
						# if u does not exist
						A_  :				Union[str, Any]	   =		[[w, v]]
					# add the other way
					if self.graph.get(_lowerCamelCase       ):
						# if there already is a edge
						if self.graph[v].count([w, u]       ) == 0:
							self.graph[v].append([w, u]       )
					else:
						# if u does not exist
						A_  :				Tuple	   =		[[w, u]]







				def 					a_  (							self   :					Dict      ,     _lowerCamelCase   :					Optional[Any]      ,     _lowerCamelCase   :					Dict       ):



					"""simple docstring"""

					if self.graph.get(_lowerCamelCase       ):
						for _ in self.graph[u]:
							if _[1] == v:
								self.graph[u].remove(_lowerCamelCase       )
        # the other way round
					if self.graph.get(_lowerCamelCase       ):
						for _ in self.graph[v]:
							if _[1] == u:
								self.graph[v].remove(_lowerCamelCase       )







				def 					a_  (							self   :					Any      ,     _lowerCamelCase   :					str=-2      ,     _lowerCamelCase   :					int=-1       ):



					"""simple docstring"""

					if s == d:
						return []
					A_  :				Optional[Any]	   =		[]
					A_  :				List[Any]	   =		[]
					if s == -2:
						A_  :				Optional[int]	   =		list(self.graph       )[0]
					stack.append(_lowerCamelCase       )
					visited.append(_lowerCamelCase       )
					A_  :				Union[str, Any]	   =		s

					while True:
						# check if there is any non isolated nodes
						if len(self.graph[s]       ) != 0:
							A_  :				Any	   =		s
							for node in self.graph[s]:
								if visited.count(node[1]       ) < 1:
									if node[1] == d:
										visited.append(_lowerCamelCase       )
										return visited
									else:
										stack.append(node[1]       )
										visited.append(node[1]       )
										A_  :				int	   =		node[1]
										break

            # check if all the children are visited
						if s == ss:
							stack.pop()
							if len(_lowerCamelCase       ) != 0:
								A_  :				List[Any]	   =		stack[len(_lowerCamelCase       ) - 1]
						else:
							A_  :				str	   =		ss

						# check if se have reached the starting point
						if len(_lowerCamelCase       ) == 0:
							return visited







				def 					a_  (							self   :					Optional[Any]      ,     _lowerCamelCase   :					Union[str, Any]=-1       ):



					"""simple docstring"""

					if c == -1:
						A_  :				List[Any]	   =		floor(random() * 1_00_00       ) + 10
					for i in range(_lowerCamelCase       ):
						# every vertex has max 100 edges
						for _ in range(floor(random() * 1_02       ) + 1       ):
							A_  :				List[str]	   =		floor(random() * c       ) + 1
							if n != i:
								self.add_pair(_lowerCamelCase      ,     _lowerCamelCase      ,     1       )







				def 					a_  (							self   :					Dict      ,     _lowerCamelCase   :					List[Any]=-2       ):



					"""simple docstring"""

					A_  :				Dict	   =		deque()
					A_  :				Tuple	   =		[]
					if s == -2:
						A_  :				List[str]	   =		list(self.graph       )[0]
					d.append(_lowerCamelCase       )
					visited.append(_lowerCamelCase       )
					while d:
						A_  :				str	   =		d.popleft()
						if len(self.graph[s]       ) != 0:
							for node in self.graph[s]:
								if visited.count(node[1]       ) < 1:
									d.append(node[1]       )
									visited.append(node[1]       )
					return visited







				def 					a_  (							self   :					Any      ,     _lowerCamelCase   :					int       ):



					"""simple docstring"""

					return len(self.graph[u]       )







				def 					a_  (							self   :					Any       ):



					"""simple docstring"""

					A_  :				Dict	   =		[]
					A_  :				Optional[int]	   =		[]
					A_  :				Union[str, Any]	   =		list(self.graph       )[0]
					stack.append(_lowerCamelCase       )
					visited.append(_lowerCamelCase       )
					A_  :				str	   =		-2
					A_  :				int	   =		[]
					A_  :				Optional[Any]	   =		s
					A_  :				str	   =		False
					A_  :				int	   =		set()

					while True:
						# check if there is any non isolated nodes
						if len(self.graph[s]       ) != 0:
							A_  :				Tuple	   =		s
							for node in self.graph[s]:
								if (
								    visited.count(node[1]       ) > 0
								    and node[1] != parent
								    and indirect_parents.count(node[1]       ) > 0
								    and not on_the_way_back
								):
									A_  :				Tuple	   =		len(_lowerCamelCase       ) - 1
									while len_stack >= 0:
										if stack[len_stack] == node[1]:
											anticipating_nodes.add(node[1]       )
											break
										else:
											anticipating_nodes.add(stack[len_stack]       )
											len_stack -= 1
								if visited.count(node[1]       ) < 1:
									stack.append(node[1]       )
									visited.append(node[1]       )
									A_  :				str	   =		node[1]
									break

            # check if all the children are visited
						if s == ss:
							stack.pop()
							A_  :				Union[str, Any]	   =		True
							if len(_lowerCamelCase       ) != 0:
								A_  :				Tuple	   =		stack[len(_lowerCamelCase       ) - 1]
						else:
							A_  :				str	   =		False
							indirect_parents.append(_lowerCamelCase       )
							A_  :				Union[str, Any]	   =		s
							A_  :				List[str]	   =		ss

						# check if se have reached the starting point
						if len(_lowerCamelCase       ) == 0:
							return list(_lowerCamelCase       )







				def 					a_  (							self   :					Union[str, Any]       ):



					"""simple docstring"""

					A_  :				List[str]	   =		[]
					A_  :				int	   =		[]
					A_  :				List[Any]	   =		list(self.graph       )[0]
					stack.append(_lowerCamelCase       )
					visited.append(_lowerCamelCase       )
					A_  :				int	   =		-2
					A_  :				Tuple	   =		[]
					A_  :				Any	   =		s
					A_  :				Tuple	   =		False
					A_  :				Union[str, Any]	   =		set()

					while True:
						# check if there is any non isolated nodes
						if len(self.graph[s]       ) != 0:
							A_  :				Any	   =		s
							for node in self.graph[s]:
								if (
								    visited.count(node[1]       ) > 0
								    and node[1] != parent
								    and indirect_parents.count(node[1]       ) > 0
								    and not on_the_way_back
								):
									A_  :				Optional[int]	   =		len(_lowerCamelCase       ) - 1
									while len_stack_minus_one >= 0:
										if stack[len_stack_minus_one] == node[1]:
											anticipating_nodes.add(node[1]       )
											break
										else:
											return True
								if visited.count(node[1]       ) < 1:
									stack.append(node[1]       )
									visited.append(node[1]       )
									A_  :				Optional[Any]	   =		node[1]
									break

            # check if all the children are visited
						if s == ss:
							stack.pop()
							A_  :				Optional[int]	   =		True
							if len(_lowerCamelCase       ) != 0:
								A_  :				str	   =		stack[len(_lowerCamelCase       ) - 1]
						else:
							A_  :				List[Any]	   =		False
							indirect_parents.append(_lowerCamelCase       )
							A_  :				List[Any]	   =		s
							A_  :				List[Any]	   =		ss

						# check if se have reached the starting point
						if len(_lowerCamelCase       ) == 0:
							return False







				def 					a_  (							self   :					Union[str, Any]       ):



					"""simple docstring"""

					return list(self.graph       )







				def 					a_  (							self   :					Tuple      ,     _lowerCamelCase   :					Union[str, Any]=-2      ,     _lowerCamelCase   :					str=-1       ):



					"""simple docstring"""

					A_  :				Optional[int]	   =		time()
					self.dfs(_lowerCamelCase      ,     _lowerCamelCase       )
					A_  :				Union[str, Any]	   =		time()
					return end - begin







				def 					a_  (							self   :					Tuple      ,     _lowerCamelCase   :					str=-2       ):



					"""simple docstring"""

					A_  :				Optional[int]	   =		time()
					self.bfs(_lowerCamelCase       )
					A_  :				List[str]	   =		time()
					return end - begin





