





'''simple docstring'''

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






class        UpperCAmelCase     :
 def __init__(    self				:Optional[Any]							)->							Union[str, Any]:
     A__    =      {}
 def 					UpperCAmelCase_							(    self				:Union[str, Any]						,	lowercase_				:List[Any]						,	lowercase_				:Optional[Any]						,	lowercase_				:Dict=1							)->							Optional[Any]:
     if self.graph.get(UpperCAmelCase_							):
         if self.graph[u].count([w, v]							) == 0:
             self.graph[u].append([w, v]							)
     else:
         A__    =      [[w, v]]
     if not self.graph.get(UpperCAmelCase_							):
         A__    =      []
 def 					UpperCAmelCase_							(    self				:List[str]							)->							Optional[Any]:
     return list(self.graph							)
 def 					UpperCAmelCase_							(    self				:int						,	lowercase_				:Any						,	lowercase_				:int							)->							List[str]:
     if self.graph.get(UpperCAmelCase_							):
         for _ in self.graph[u]:
             if _[1] == v:
                 self.graph[u].remove(UpperCAmelCase_							)
 def 					UpperCAmelCase_							(    self				:List[Any]						,	lowercase_				:Optional[Any]=-2						,	lowercase_				:int=-1							)->							Tuple:
     if s == d:
         return []
     A__    =      []
     A__    =      []
     if s == -2:
         A__    =      list(self.graph							)[0]
     stack.append(UpperCAmelCase_							)
     visited.append(UpperCAmelCase_							)
     A__    =      s

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

            # check if all the children are visited
         if s == ss:
             stack.pop()
             if len(UpperCAmelCase_							) != 0:
                 A__    =      stack[len(UpperCAmelCase_							) - 1]
         else:
             A__    =      ss

         # check if se have reached the starting point
         if len(UpperCAmelCase_							) == 0:
             return visited
 def 					UpperCAmelCase_							(    self				:Optional[Any]						,	lowercase_				:str=-1							)->							str:
     if c == -1:
         A__    =      floor(random() * 1_00_00							) + 10
     for i in range(UpperCAmelCase_							):
         # every vertex has max 100 edges
         for _ in range(floor(random() * 1_02							) + 1							):
             A__    =      floor(random() * c							) + 1
             if n != i:
                 self.add_pair(UpperCAmelCase_						,	UpperCAmelCase_						,	1							)
 def 					UpperCAmelCase_							(    self				:Optional[int]						,	lowercase_				:int=-2							)->							Dict:
     A__    =      deque()
     A__    =      []
     if s == -2:
         A__    =      list(self.graph							)[0]
     d.append(UpperCAmelCase_							)
     visited.append(UpperCAmelCase_							)
     while d:
         A__    =      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 					UpperCAmelCase_							(    self				:List[Any]						,	lowercase_				:List[str]							)->							Union[str, Any]:
     A__    =      0
     for x in self.graph:
         for y in self.graph[x]:
             if y[1] == u:
                 count += 1
     return count
 def 					UpperCAmelCase_							(    self				:Tuple						,	lowercase_				:List[Any]							)->							Union[str, Any]:
     return len(self.graph[u]							)
 def 					UpperCAmelCase_							(    self				:List[str]						,	lowercase_				:str=-2							)->							Union[str, Any]:
     A__    =      []
     A__    =      []
     if s == -2:
         A__    =      list(self.graph							)[0]
     stack.append(UpperCAmelCase_							)
     visited.append(UpperCAmelCase_							)
     A__    =      s
     A__    =      []

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

            # check if all the children are visited
         if s == ss:
             sorted_nodes.append(stack.pop()							)
             if len(UpperCAmelCase_							) != 0:
                 A__    =      stack[len(UpperCAmelCase_							) - 1]
         else:
             A__    =      ss

         # check if se have reached the starting point
         if len(UpperCAmelCase_							) == 0:
             return sorted_nodes
 def 					UpperCAmelCase_							(    self				:Optional[Any]							)->							Union[str, Any]:
     A__    =      []
     A__    =      []
     A__    =      list(self.graph							)[0]
     stack.append(UpperCAmelCase_							)
     visited.append(UpperCAmelCase_							)
     A__    =      -2
     A__    =      []
     A__    =      s
     A__    =      False
     A__    =      set()

     while True:
         # check if there is any non isolated nodes
         if len(self.graph[s]							) != 0:
             A__    =      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__    =      len(UpperCAmelCase_							) - 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__    =      node[1]
                     break

            # check if all the children are visited
         if s == ss:
             stack.pop()
             A__    =      True
             if len(UpperCAmelCase_							) != 0:
                 A__    =      stack[len(UpperCAmelCase_							) - 1]
         else:
             A__    =      False
             indirect_parents.append(UpperCAmelCase_							)
             A__    =      s
             A__    =      ss

         # check if se have reached the starting point
         if len(UpperCAmelCase_							) == 0:
             return list(UpperCAmelCase_							)
 def 					UpperCAmelCase_							(    self				:Tuple							)->							Optional[int]:
     A__    =      []
     A__    =      []
     A__    =      list(self.graph							)[0]
     stack.append(UpperCAmelCase_							)
     visited.append(UpperCAmelCase_							)
     A__    =      -2
     A__    =      []
     A__    =      s
     A__    =      False
     A__    =      set()

     while True:
         # check if there is any non isolated nodes
         if len(self.graph[s]							) != 0:
             A__    =      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__    =      len(UpperCAmelCase_							) - 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__    =      node[1]
                     break

            # check if all the children are visited
         if s == ss:
             stack.pop()
             A__    =      True
             if len(UpperCAmelCase_							) != 0:
                 A__    =      stack[len(UpperCAmelCase_							) - 1]
         else:
             A__    =      False
             indirect_parents.append(UpperCAmelCase_							)
             A__    =      s
             A__    =      ss

         # check if se have reached the starting point
         if len(UpperCAmelCase_							) == 0:
             return False
 def 					UpperCAmelCase_							(    self				:List[Any]						,	lowercase_				:Any=-2						,	lowercase_				:List[str]=-1							)->							List[Any]:
     A__    =      time()
     self.dfs(UpperCAmelCase_						,	UpperCAmelCase_							)
     A__    =      time()
     return end - begin



 def 					UpperCAmelCase_							(    self				:List[Any]						,	lowercase_				:List[str]=-2							)->							Dict:
     A__    =      time()
     self.bfs(UpperCAmelCase_							)
     A__    =      time()
     return end - begin






class        UpperCAmelCase     :
 def __init__(    self				:str							)->							Optional[Any]:
     A__    =      {}
 def 					UpperCAmelCase_							(    self				:int						,	lowercase_				:int						,	lowercase_				:List[str]						,	lowercase_				:Dict=1							)->							Any:
     # check if the u exists
     if self.graph.get(UpperCAmelCase_							):
         # 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__    =      [[w, v]]
     # add the other way
     if self.graph.get(UpperCAmelCase_							):
         # 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__    =      [[w, u]]
 def 					UpperCAmelCase_							(    self				:Dict						,	lowercase_				:Optional[int]						,	lowercase_				:Any							)->							Any:
     if self.graph.get(UpperCAmelCase_							):
         for _ in self.graph[u]:
             if _[1] == v:
                 self.graph[u].remove(UpperCAmelCase_							)
        # the other way round
     if self.graph.get(UpperCAmelCase_							):
         for _ in self.graph[v]:
             if _[1] == u:
                 self.graph[v].remove(UpperCAmelCase_							)
 def 					UpperCAmelCase_							(    self				:Tuple						,	lowercase_				:List[str]=-2						,	lowercase_				:str=-1							)->							Any:
     if s == d:
         return []
     A__    =      []
     A__    =      []
     if s == -2:
         A__    =      list(self.graph							)[0]
     stack.append(UpperCAmelCase_							)
     visited.append(UpperCAmelCase_							)
     A__    =      s

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

            # check if all the children are visited
         if s == ss:
             stack.pop()
             if len(UpperCAmelCase_							) != 0:
                 A__    =      stack[len(UpperCAmelCase_							) - 1]
         else:
             A__    =      ss

         # check if se have reached the starting point
         if len(UpperCAmelCase_							) == 0:
             return visited
 def 					UpperCAmelCase_							(    self				:Optional[Any]						,	lowercase_				:Tuple=-1							)->							Tuple:
     if c == -1:
         A__    =      floor(random() * 1_00_00							) + 10
     for i in range(UpperCAmelCase_							):
         # every vertex has max 100 edges
         for _ in range(floor(random() * 1_02							) + 1							):
             A__    =      floor(random() * c							) + 1
             if n != i:
                 self.add_pair(UpperCAmelCase_						,	UpperCAmelCase_						,	1							)
 def 					UpperCAmelCase_							(    self				:Optional[Any]						,	lowercase_				:Dict=-2							)->							List[str]:
     A__    =      deque()
     A__    =      []
     if s == -2:
         A__    =      list(self.graph							)[0]
     d.append(UpperCAmelCase_							)
     visited.append(UpperCAmelCase_							)
     while d:
         A__    =      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 					UpperCAmelCase_							(    self				:Optional[Any]						,	lowercase_				:List[str]							)->							Any:
     return len(self.graph[u]							)
 def 					UpperCAmelCase_							(    self				:Any							)->							List[str]:
     A__    =      []
     A__    =      []
     A__    =      list(self.graph							)[0]
     stack.append(UpperCAmelCase_							)
     visited.append(UpperCAmelCase_							)
     A__    =      -2
     A__    =      []
     A__    =      s
     A__    =      False
     A__    =      set()

     while True:
         # check if there is any non isolated nodes
         if len(self.graph[s]							) != 0:
             A__    =      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__    =      len(UpperCAmelCase_							) - 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__    =      node[1]
                     break

            # check if all the children are visited
         if s == ss:
             stack.pop()
             A__    =      True
             if len(UpperCAmelCase_							) != 0:
                 A__    =      stack[len(UpperCAmelCase_							) - 1]
         else:
             A__    =      False
             indirect_parents.append(UpperCAmelCase_							)
             A__    =      s
             A__    =      ss

         # check if se have reached the starting point
         if len(UpperCAmelCase_							) == 0:
             return list(UpperCAmelCase_							)
 def 					UpperCAmelCase_							(    self				:Optional[Any]							)->							Union[str, Any]:
     A__    =      []
     A__    =      []
     A__    =      list(self.graph							)[0]
     stack.append(UpperCAmelCase_							)
     visited.append(UpperCAmelCase_							)
     A__    =      -2
     A__    =      []
     A__    =      s
     A__    =      False
     A__    =      set()

     while True:
         # check if there is any non isolated nodes
         if len(self.graph[s]							) != 0:
             A__    =      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__    =      len(UpperCAmelCase_							) - 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__    =      node[1]
                     break

            # check if all the children are visited
         if s == ss:
             stack.pop()
             A__    =      True
             if len(UpperCAmelCase_							) != 0:
                 A__    =      stack[len(UpperCAmelCase_							) - 1]
         else:
             A__    =      False
             indirect_parents.append(UpperCAmelCase_							)
             A__    =      s
             A__    =      ss

         # check if se have reached the starting point
         if len(UpperCAmelCase_							) == 0:
             return False
 def 					UpperCAmelCase_							(    self				:List[Any]							)->							str:
     return list(self.graph							)
 def 					UpperCAmelCase_							(    self				:Dict						,	lowercase_				:Optional[int]=-2						,	lowercase_				:Tuple=-1							)->							Tuple:
     A__    =      time()
     self.dfs(UpperCAmelCase_						,	UpperCAmelCase_							)
     A__    =      time()
     return end - begin



 def 					UpperCAmelCase_							(    self				:int						,	lowercase_				:Union[str, Any]=-2							)->							int:
     A__    =      time()
     self.bfs(UpperCAmelCase_							)
     A__    =      time()
     return end - begin










'''simple docstring'''

from .imports import is_tqdm_available


if is_tqdm_available():
  from tqdm.auto import tqdm as _tqdm

from ..state import PartialState





def 						UpperCamelCase    (							_lowerCamelCase : bool = True						,	*_lowerCamelCase : Optional[int]						,	**_lowerCamelCase : Optional[Any]	):
    if not is_tqdm_available():
        raise ImportError("Accelerate's `tqdm` module requires `tqdm` to be installed. Please run `pip install tqdm`."	)
    A__    =      False
    if main_process_only:
        A__    =      PartialState().local_process_index == 0
    return _tqdm(*_lowerCamelCase						,	**_lowerCamelCase						,	disable=_lowerCamelCase	)



