




"""simple docstring"""

import argparse
import json
from pathlib import Path

import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image

from transformers import YolosConfig, YolosForObjectDetection, YolosImageProcessor
from transformers.utils import logging


logging.set_verbosity_info()
_lowerCamelCase  :			Dict        =       logging.get_logger(__name__)

def 		lowercase_      (		_UpperCAmelCase	):




	"""simple docstring"""






	A_  :				Optional[Any]	   =		YolosConfig()

	# size of the architecture
	if "yolos_ti" in yolos_name:
		A_  :				Union[str, Any]	   =		192
		A_  :				Dict	   =		768
		A_  :				int	   =		12
		A_  :				Union[str, Any]	   =		3
		A_  :				int	   =		[800, 1333]
		A_  :				str	   =		False
	elif yolos_name == "yolos_s_dWr":
		A_  :				Dict	   =		330
		A_  :				Union[str, Any]	   =		14
		A_  :				Dict	   =		6
		A_  :				Dict	   =		1320
	elif "yolos_s" in yolos_name:
		A_  :				Dict	   =		384
		A_  :				List[str]	   =		1536
		A_  :				Optional[int]	   =		12
		A_  :				Union[str, Any]	   =		6
	elif "yolos_b" in yolos_name:
		A_  :				Optional[int]	   =		[800, 1344]

	A_  :				List[Any]	   =		91
	A_  :				Optional[int]	   =		'''huggingface/label-files'''
	A_  :				str	   =		'''coco-detection-id2label.json'''
	A_  :				List[str]	   =		json.load(open(hf_hub_download(_UpperCAmelCase  ,      _UpperCAmelCase  ,      repo_type='''dataset'''	)  ,      '''r'''	)	)
	A_  :				Any	   =		{int(_UpperCAmelCase	): v for k, v in idalabel.items()}
	A_  :				Union[str, Any]	   =		idalabel
	A_  :				List[Any]	   =		{v: k for k, v in idalabel.items()}

	return config

def 		lowercase_      (		_UpperCAmelCase  ,      _UpperCAmelCase  ,      _UpperCAmelCase = False	):




	"""simple docstring"""






	for i in range(config.num_hidden_layers	):
		# read in weights + bias of input projection layer (in timm, this is a single matrix + bias)
		A_  :				List[str]	   =		state_dict.pop(f"""blocks.{i}.attn.qkv.weight"""	)
		A_  :				List[str]	   =		state_dict.pop(f"""blocks.{i}.attn.qkv.bias"""	)
		# next, add query, keys and values (in that order) to the state dict
		A_  :				int	   =		in_proj_weight[: config.hidden_size, :]
		A_  :				Any	   =		in_proj_bias[: config.hidden_size]
		A_  :				Union[str, Any]	   =		in_proj_weight[
		    config.hidden_size : config.hidden_size * 2, :
		]
		A_  :				Union[str, Any]	   =		in_proj_bias[
		    config.hidden_size : config.hidden_size * 2
		]
		A_  :				Union[str, Any]	   =		in_proj_weight[-config.hidden_size :, :]
		A_  :				Any	   =		in_proj_bias[-config.hidden_size :]

def 		lowercase_      (		_UpperCAmelCase	):




	"""simple docstring"""






	if "backbone" in name:
		A_  :				int	   =		name.replace('''backbone'''  ,      '''vit'''	)
	if "cls_token" in name:
		A_  :				List[str]	   =		name.replace('''cls_token'''  ,      '''embeddings.cls_token'''	)
	if "det_token" in name:
		A_  :				Any	   =		name.replace('''det_token'''  ,      '''embeddings.detection_tokens'''	)
	if "mid_pos_embed" in name:
		A_  :				Union[str, Any]	   =		name.replace('''mid_pos_embed'''  ,      '''encoder.mid_position_embeddings'''	)
	if "pos_embed" in name:
		A_  :				List[str]	   =		name.replace('''pos_embed'''  ,      '''embeddings.position_embeddings'''	)
	if "patch_embed.proj" in name:
		A_  :				str	   =		name.replace('''patch_embed.proj'''  ,      '''embeddings.patch_embeddings.projection'''	)
	if "blocks" in name:
		A_  :				List[Any]	   =		name.replace('''blocks'''  ,      '''encoder.layer'''	)
	if "attn.proj" in name:
		A_  :				Union[str, Any]	   =		name.replace('''attn.proj'''  ,      '''attention.output.dense'''	)
	if "attn" in name:
		A_  :				Tuple	   =		name.replace('''attn'''  ,      '''attention.self'''	)
	if "norm1" in name:
		A_  :				List[Any]	   =		name.replace('''norm1'''  ,      '''layernorm_before'''	)
	if "norm2" in name:
		A_  :				Optional[Any]	   =		name.replace('''norm2'''  ,      '''layernorm_after'''	)
	if "mlp.fc1" in name:
		A_  :				Optional[Any]	   =		name.replace('''mlp.fc1'''  ,      '''intermediate.dense'''	)
	if "mlp.fc2" in name:
		A_  :				Union[str, Any]	   =		name.replace('''mlp.fc2'''  ,      '''output.dense'''	)
	if "class_embed" in name:
		A_  :				Union[str, Any]	   =		name.replace('''class_embed'''  ,      '''class_labels_classifier'''	)
	if "bbox_embed" in name:
		A_  :				List[str]	   =		name.replace('''bbox_embed'''  ,      '''bbox_predictor'''	)
	if "vit.norm" in name:
		A_  :				Optional[Any]	   =		name.replace('''vit.norm'''  ,      '''vit.layernorm'''	)

	return name

def 		lowercase_      (		_UpperCAmelCase  ,      _UpperCAmelCase	):




	"""simple docstring"""






	for key in orig_state_dict.copy().keys():
		A_  :				Dict	   =		orig_state_dict.pop(_UpperCAmelCase	)

		if "qkv" in key:
			A_  :				Any	   =		key.split('''.'''	)
			A_  :				List[Any]	   =		int(key_split[2]	)
			A_  :				List[str]	   =		model.vit.encoder.layer[layer_num].attention.attention.all_head_size
			if "weight" in key:
				A_  :				Any	   =		val[:dim, :]
				A_  :				Optional[Any]	   =		val[
				    dim : dim * 2, :
				]
				A_  :				Tuple	   =		val[-dim:, :]
			else:
				A_  :				Optional[Any]	   =		val[:dim]
				A_  :				Optional[int]	   =		val[dim : dim * 2]
				A_  :				List[str]	   =		val[-dim:]
		else:
			A_  :				Union[str, Any]	   =		val

	return orig_state_dict

def 		lowercase_      (		):




	"""simple docstring"""






	A_  :				List[Any]	   =		'''http://images.cocodataset.org/val2017/000000039769.jpg'''
	A_  :				str	   =		Image.open(requests.get(_UpperCAmelCase  ,      stream=_UpperCAmelCase	).raw	)
	return im

@torch.no_grad()
def 		lowercase_      (		_UpperCAmelCase  ,      _UpperCAmelCase  ,      _UpperCAmelCase  ,      _UpperCAmelCase = False	):




	"""simple docstring"""






	A_  :				Optional[int]	   =		get_yolos_config(_UpperCAmelCase	)

	# load original state_dict
	A_  :				List[Any]	   =		torch.load(_UpperCAmelCase  ,      map_location='''cpu'''	)['''model''']

	# load 🤗 model
	A_  :				Dict	   =		YolosForObjectDetection(_UpperCAmelCase	)
	model.eval()
	A_  :				List[str]	   =		convert_state_dict(_UpperCAmelCase  ,      _UpperCAmelCase	)
	model.load_state_dict(_UpperCAmelCase	)

	# Check outputs on an image, prepared by YolosImageProcessor
	A_  :				Optional[Any]	   =		800 if yolos_name != '''yolos_ti''' else 512
	A_  :				Union[str, Any]	   =		YolosImageProcessor(format='''coco_detection'''  ,      size=_UpperCAmelCase	)
	A_  :				Tuple	   =		image_processor(images=prepare_img()  ,      return_tensors='''pt'''	)
	A_  :				str	   =		model(**_UpperCAmelCase	)
	A_   , A_  :				int	   =		outputs.logits, outputs.pred_boxes

	A_   , A_  :				Any	   =		None, None
	if yolos_name == "yolos_ti":
		A_  :				Dict	   =		torch.tensor(
		    [[-39.5022, -11.9820, -17.6888], [-29.9574, -9.9769, -17.7691], [-42.3281, -20.7200, -30.6294]]	)
		A_  :				Optional[Any]	   =		torch.tensor(
		    [[0.4021, 0.0836, 0.7979], [0.0184, 0.2609, 0.0364], [0.1781, 0.2004, 0.2095]]	)
	elif yolos_name == "yolos_s_200_pre":
		A_  :				Union[str, Any]	   =		torch.tensor(
		    [[-24.0248, -10.3024, -14.8290], [-42.0392, -16.8200, -27.4334], [-27.2743, -11.8154, -18.7148]]	)
		A_  :				str	   =		torch.tensor(
		    [[0.2559, 0.5455, 0.4706], [0.2989, 0.7279, 0.1875], [0.7732, 0.4017, 0.4462]]	)
	elif yolos_name == "yolos_s_300_pre":
		A_  :				str	   =		torch.tensor(
		    [[-36.2220, -14.4385, -23.5457], [-35.6970, -14.7583, -21.3935], [-31.5939, -13.6042, -16.8049]]	)
		A_  :				int	   =		torch.tensor(
		    [[0.7614, 0.2316, 0.4728], [0.7168, 0.4495, 0.3855], [0.4996, 0.1466, 0.9996]]	)
	elif yolos_name == "yolos_s_dWr":
		A_  :				int	   =		torch.tensor(
		    [[-42.8668, -24.1049, -41.1690], [-34.7456, -14.1274, -24.9194], [-33.7898, -12.1946, -25.6495]]	)
		A_  :				Dict	   =		torch.tensor(
		    [[0.5587, 0.2773, 0.0605], [0.5004, 0.3014, 0.9994], [0.4999, 0.1548, 0.9994]]	)
	elif yolos_name == "yolos_base":
		A_  :				Optional[Any]	   =		torch.tensor(
		    [[-40.6064, -24.3084, -32.6447], [-55.1990, -30.7719, -35.5877], [-51.4311, -33.3507, -35.6462]]	)
		A_  :				str	   =		torch.tensor(
		    [[0.5555, 0.2794, 0.0655], [0.9049, 0.2664, 0.1894], [0.9183, 0.1984, 0.1635]]	)
	else:
		raise ValueError(f"""Unknown yolos_name: {yolos_name}"""	)

	assert torch.allclose(logits[0, :3, :3]  ,      _UpperCAmelCase  ,      atol=1E-4	)
	assert torch.allclose(pred_boxes[0, :3, :3]  ,      _UpperCAmelCase  ,      atol=1E-4	)

	Path(_UpperCAmelCase	).mkdir(exist_ok=_UpperCAmelCase	)
	print(f"""Saving model {yolos_name} to {pytorch_dump_folder_path}"""	)
	model.save_pretrained(_UpperCAmelCase	)
	print(f"""Saving image processor to {pytorch_dump_folder_path}"""	)
	image_processor.save_pretrained(_UpperCAmelCase	)

	if push_to_hub:
		A_  :				Tuple	   =		{
		    '''yolos_ti''': '''yolos-tiny''',
		    '''yolos_s_200_pre''': '''yolos-small''',
		    '''yolos_s_300_pre''': '''yolos-small-300''',
		    '''yolos_s_dWr''': '''yolos-small-dwr''',
		    '''yolos_base''': '''yolos-base''',
		}

		print('''Pushing to the hub...'''	)
		A_  :				Optional[Any]	   =		model_mapping[yolos_name]
		image_processor.push_to_hub(_UpperCAmelCase  ,      organization='''hustvl'''	)
		model.push_to_hub(_UpperCAmelCase  ,      organization='''hustvl'''	)


if __name__ == "__main__":
		_lowerCamelCase  :			str        =       argparse.ArgumentParser()
		# Required parameters
		parser.add_argument(
		    '--yolos_name',
		    default='yolos_s_200_pre',
		    type=str,
		    help=(
		        'Name of the YOLOS model you\'d like to convert. Should be one of \'yolos_ti\', \'yolos_s_200_pre\','
		        ' \'yolos_s_300_pre\', \'yolos_s_dWr\', \'yolos_base\'.'
		    ),
		)
		parser.add_argument(
		    '--checkpoint_path', default=None, type=str, help='Path to the original state dict (.pth file).'
		)
		parser.add_argument(
		    '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.'
		)
		parser.add_argument(
		    '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the 🤗 hub.'
		)

		_lowerCamelCase  :			List[str]        =       parser.parse_args()
		convert_yolos_checkpoint(args.yolos_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub)











"""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





