
import unittest

from transformers import ErnieMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import (
     ErnieMForInformationExtraction,
     ErnieMForMultipleChoice,
     ErnieMForQuestionAnswering,
     ErnieMForSequenceClassification,
     ErnieMForTokenClassification,
     ErnieMModel,
 )
 from transformers.models.ernie_m.modeling_ernie_m import ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST






class 	__magic_name__				:






     def __init__(							self		,	_lowercase		,	_lowercase=13		,	_lowercase=7		,	_lowercase=True		,	_lowercase=True		,	_lowercase=True		,	_lowercase=99		,	_lowercase=32		,	_lowercase=5		,	_lowercase=4		,	_lowercase=37		,	_lowercase="gelu"		,	_lowercase=0.1		,	_lowercase=0.1		,	_lowercase=512		,	_lowercase=16		,	_lowercase=2		,	_lowercase=0.02		,	_lowercase=3		,	_lowercase=4		,	_lowercase=None		,	)->    List[str]:
         UpperCamelCase_      =	parent
         UpperCamelCase_      =	batch_size
         UpperCamelCase_      =	seq_length
         UpperCamelCase_      =	is_training
         UpperCamelCase_      =	use_input_mask
         UpperCamelCase_      =	use_labels
         UpperCamelCase_      =	vocab_size
         UpperCamelCase_      =	hidden_size
         UpperCamelCase_      =	num_hidden_layers
         UpperCamelCase_      =	num_attention_heads
         UpperCamelCase_      =	intermediate_size
         UpperCamelCase_      =	hidden_act
         UpperCamelCase_      =	hidden_dropout_prob
         UpperCamelCase_      =	attention_probs_dropout_prob
         UpperCamelCase_      =	max_position_embeddings
         UpperCamelCase_      =	type_vocab_size
         UpperCamelCase_      =	type_sequence_label_size
         UpperCamelCase_      =	initializer_range
         UpperCamelCase_      =	num_labels
         UpperCamelCase_      =	num_choices
         UpperCamelCase_      =	scope






     def   UpperCAmelCase_							(							self  )->    Tuple:
         UpperCamelCase_      =	ids_tensor([self.batch_size, self.seq_length]		,	self.vocab_size  )

         UpperCamelCase_      =	None
         if self.use_input_mask:
             UpperCamelCase_      =	random_attention_mask([self.batch_size, self.seq_length]  )

         UpperCamelCase_      =	None
         UpperCamelCase_      =	None
         UpperCamelCase_      =	None
         if self.use_labels:
             UpperCamelCase_      =	ids_tensor([self.batch_size]		,	self.type_sequence_label_size  )
             UpperCamelCase_      =	ids_tensor([self.batch_size, self.seq_length]		,	self.num_labels  )
             UpperCamelCase_      =	ids_tensor([self.batch_size]		,	self.num_choices  )

         UpperCamelCase_      =	self.get_config()

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






     def   UpperCAmelCase_							(							self  )->    Union[str, Any]:
         UpperCamelCase_      =	ids_tensor([self.batch_size, self.seq_length]		,	self.vocab_size  )

         UpperCamelCase_      =	None
         if self.use_input_mask:
             UpperCamelCase_      =	random_attention_mask([self.batch_size, self.seq_length]  )
         UpperCamelCase_      =	self.get_config()

         return config, input_ids, input_mask






     def   UpperCAmelCase_							(							self  )->    Any:
         return ErnieMConfig(
             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		,	initializer_range=self.initializer_range		,	)






     def   UpperCAmelCase_							(							self		,	_lowercase		,	_lowercase		,	_lowercase		,	_lowercase		,	_lowercase		,	_lowercase  )->    Union[str, Any]:
         UpperCamelCase_      =	ErnieMModel(config=_A  )
         model.to(_A  )
         model.eval()
         UpperCamelCase_      =	model(_A		,	return_dict=_A  )
         self.parent.assertEqual(result.last_hidden_state.shape		,	(self.batch_size, self.seq_length, self.hidden_size)  )






     def   UpperCAmelCase_							(							self		,	_lowercase		,	_lowercase		,	_lowercase		,	_lowercase		,	_lowercase		,	_lowercase  )->    Tuple:
         UpperCamelCase_      =	ErnieMForQuestionAnswering(config=_A  )
         model.to(_A  )
         model.eval()
         UpperCamelCase_      =	model(
             _A		,	attention_mask=_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   UpperCAmelCase_							(							self		,	_lowercase		,	_lowercase		,	_lowercase		,	_lowercase		,	_lowercase		,	_lowercase  )->    Optional[Any]:
         UpperCamelCase_      =	ErnieMForInformationExtraction(config=_A  )
         model.to(_A  )
         model.eval()
         UpperCamelCase_      =	torch.ones_like(_A		,	dtype=torch.floataa  )
         UpperCamelCase_      =	model(
             _A		,	attention_mask=_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   UpperCAmelCase_							(							self		,	_lowercase		,	_lowercase		,	_lowercase		,	_lowercase		,	_lowercase		,	_lowercase  )->    int:
         UpperCamelCase_      =	self.num_labels
         UpperCamelCase_      =	ErnieMForSequenceClassification(_A  )
         model.to(_A  )
         model.eval()
         UpperCamelCase_      =	model(_A		,	attention_mask=_A		,	labels=_A  )
         self.parent.assertEqual(result.logits.shape		,	(self.batch_size, self.num_labels)  )






     def   UpperCAmelCase_							(							self		,	_lowercase		,	_lowercase		,	_lowercase		,	_lowercase		,	_lowercase		,	_lowercase  )->    Dict:
         UpperCamelCase_      =	self.num_labels
         UpperCamelCase_      =	ErnieMForTokenClassification(config=_A  )
         model.to(_A  )
         model.eval()
         input_ids.to(_A  )
         input_mask.to(_A  )
         token_labels.to(_A  )

         UpperCamelCase_      =	model(_A		,	attention_mask=_A		,	labels=_A  )

         self.parent.assertEqual(result.logits.shape		,	(self.batch_size, self.seq_length, self.num_labels)  )






     def   UpperCAmelCase_							(							self		,	_lowercase		,	_lowercase		,	_lowercase		,	_lowercase		,	_lowercase		,	_lowercase  )->    List[str]:
         UpperCamelCase_      =	self.num_choices
         UpperCamelCase_      =	ErnieMForMultipleChoice(config=_A  )
         model.to(_A  )
         model.eval()
         UpperCamelCase_      =	input_ids.unsqueeze(1  ).expand(-1		,	self.num_choices		,	-1  ).contiguous()
         UpperCamelCase_      =	input_mask.unsqueeze(1  ).expand(-1		,	self.num_choices		,	-1  ).contiguous()
         UpperCamelCase_      =	model(
             _A		,	attention_mask=_A		,	labels=_A		,	)
         self.parent.assertEqual(result.logits.shape		,	(self.batch_size, self.num_choices)  )






     def   UpperCAmelCase_							(							self  )->    Any:
         UpperCamelCase_      =	self.prepare_config_and_inputs()
         (
             UpperCamelCase_       
         )      =	config_and_inputs
         UpperCamelCase_      =	{'input_ids': input_ids, 'attention_mask': input_mask}
         return config, inputs_dict






@require_torch
class 	__magic_name__				(       lowercase__ ,			lowercase__ ,			unittest.TestCase							):
     UpperCamelCase_  :Optional[int]								=			(
         (
             ErnieMModel,
             ErnieMForMultipleChoice,
             ErnieMForQuestionAnswering,
             ErnieMForSequenceClassification,
             ErnieMForTokenClassification,
         )
         if is_torch_available()
         else ()
     )
     UpperCamelCase_  :str								=			()
     UpperCamelCase_  :Optional[int]								=			(
         {
             """feature-extraction""": ErnieMModel,
             """question-answering""": ErnieMForQuestionAnswering,
             """text-classification""": ErnieMForSequenceClassification,
             """token-classification""": ErnieMForTokenClassification,
             """zero-shot""": ErnieMForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     UpperCamelCase_  :Tuple								=			False






     def   UpperCAmelCase_							(							self		,	_lowercase		,	_lowercase		,	_lowercase		,	_lowercase		,	_lowercase  )->    Any:
         if pipeline_test_casse_name == "QAPipelineTests":
             return True

         return False






     def   UpperCAmelCase_							(							self  )->    Tuple:
         UpperCamelCase_      =	ErnieMModelTester(self  )
         UpperCamelCase_      =	ConfigTester(self		,	config_class=_A		,	hidden_size=37  )






     def   UpperCAmelCase_							(							self  )->    int:
         self.config_tester.run_common_tests()






     def   UpperCAmelCase_							(							self  )->    Optional[int]:
         UpperCamelCase_      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*_A  )






     def   UpperCAmelCase_							(							self  )->    Dict:
         UpperCamelCase_      =	self.model_tester.prepare_config_and_inputs()
         for type in ["absolute", "relative_key", "relative_key_query"]:
             UpperCamelCase_      =	type
             self.model_tester.create_and_check_model(*_A  )






     def   UpperCAmelCase_							(							self  )->    int:
         UpperCamelCase_      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_multiple_choice(*_A  )






     def   UpperCAmelCase_							(							self  )->    Any:
         UpperCamelCase_      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*_A  )






     def   UpperCAmelCase_							(							self  )->    str:
         UpperCamelCase_      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*_A  )






     def   UpperCAmelCase_							(							self  )->    Dict:
         UpperCamelCase_      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_information_extraction(*_A  )






     def   UpperCAmelCase_							(							self  )->    Dict:
         UpperCamelCase_      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*_A  )






     @slow
     def   UpperCAmelCase_							(							self  )->    Any:
         for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             UpperCamelCase_      =	ErnieMModel.from_pretrained(_A  )
             self.assertIsNotNone(_A  )






@require_torch
class 	__magic_name__				(       unittest.TestCase							):






     @slow
     def   UpperCAmelCase_							(							self  )->    Optional[Any]:
         UpperCamelCase_      =	ErnieMModel.from_pretrained("susnato/ernie-m-base_pytorch"  )
         model.eval()
         UpperCamelCase_      =	torch.tensor([[0, 1, 2, 3, 4, 5]]  )
         UpperCamelCase_      =	model(_A  )[0]

         # TODO Replace vocab size
         UpperCamelCase_      =	768

         UpperCamelCase_      =	torch.Size((1, 6, hidden_size)  )
         self.assertEqual(output.shape		,	_A  )

         UpperCamelCase_      =	torch.tensor(
             [[[-0.0_012, 0.1_245, -0.0_214], [-0.0_742, 0.0_244, -0.0_771], [-0.0_333, 0.1_164, -0.1_554]]]  )

         self.assertTrue(torch.allclose(output[:, :3, :3]		,	_A		,	atol=1e-3  )  )



from __future__ import annotations




def  lowerCAmelCase(			SCREAMING_SNAKE_CASE_ )->	str:



    """simple docstring"""






    global graph, reversed_graph, scc, component, visit, stack
    if visit[u]:
        return
    UpperCamelCase_      =	True
    for v in graph[u]:
        dfs(SCREAMING_SNAKE_CASE_ )
    stack.append(SCREAMING_SNAKE_CASE_ )




def  lowerCAmelCase(			SCREAMING_SNAKE_CASE_ )->	Dict:



    """simple docstring"""






    global graph, reversed_graph, scc, component, visit, stack
    if visit[u]:
        return
    UpperCamelCase_      =	True
    component.append(SCREAMING_SNAKE_CASE_ )
    for v in reversed_graph[u]:
        dfsa(SCREAMING_SNAKE_CASE_ )




def  lowerCAmelCase(			)->	Dict:



    """simple docstring"""






    global graph, reversed_graph, scc, component, visit, stack
    for i in range(SCREAMING_SNAKE_CASE_ ):
        dfs(SCREAMING_SNAKE_CASE_ )
    UpperCamelCase_      =	[False] * n
    for i in stack[::-1]:
        if visit[i]:
            continue
        UpperCamelCase_      =	[]
        dfsa(SCREAMING_SNAKE_CASE_ )
        scc.append(SCREAMING_SNAKE_CASE_ )
    return scc


if __name__ == "__main__":
 # n - no of nodes, m - no of edges
 SCREAMING_SNAKE_CASE  ,       SCREAMING_SNAKE_CASE		:str				  =    list(map(int, input().strip().split()))

 SCREAMING_SNAKE_CASE		:list[list[int]]				  =    [[] for _ in range(n)]  # graph
 SCREAMING_SNAKE_CASE		:list[list[int]]				  =    [[] for i in range(n)]  # reversed graph
 # input graph data (edges)
 for _ in range(m):
  SCREAMING_SNAKE_CASE  ,       SCREAMING_SNAKE_CASE		:str				  =    list(map(int, input().strip().split()))
  graph[u].append(v)
  reversed_graph[v].append(u)

 SCREAMING_SNAKE_CASE		:list[int]				  =    []
 SCREAMING_SNAKE_CASE		:list[bool]				  =    [False] * n
 SCREAMING_SNAKE_CASE		:list[int]				  =    []
 SCREAMING_SNAKE_CASE		:list[int]				  =    []
 print(kosaraju())
