from __future__ import annotations

from queue import Queue


def     UpperCamelCase(						__UpperCamelCase							:	int  ,__UpperCamelCase							:	int				):
		a ^= b
		b ^= a
		a ^= b
		return a, b


def     UpperCamelCase(						__UpperCamelCase							:	int  ,__UpperCamelCase							:	list[list[int]]				):
		lowerCAmelCase_    :  str             =				1
		while (1 << j) < max_node:
				for i in range(1  ,max_node + 1				):
						lowerCAmelCase_    :  List[str]             =				parent[j - 1][parent[j - 1][i]]
				j += 1
		return parent


def     UpperCamelCase(						__UpperCamelCase							:	int  ,__UpperCamelCase							:	int  ,__UpperCamelCase							:	list[int]  ,__UpperCamelCase							:	list[list[int]]				):
		# u must be deeper in the tree than v
		if level[u] < level[v]:
				lowerCAmelCase_    :  List[Any]             =				swap(UpperCamelCase__  ,UpperCamelCase__				)
		# making depth of u same as depth of v
		for i in range(18  ,-1  ,-1				):
				if level[u] - (1 << i) >= level[v]:
						lowerCAmelCase_    :  Tuple             =				parent[i][u]
    # at the same depth if u==v that mean lca is found
		if u == v:
				return u
		# moving both nodes upwards till lca in found
		for i in range(18  ,-1  ,-1				):
				if parent[i][u] not in [0, parent[i][v]]:
						lowerCAmelCase_    :  List[Any]             =				parent[i][u], parent[i][v]
    # returning longest common ancestor of u,v
		return parent[0][u]


def     UpperCamelCase(						__UpperCamelCase							:	list[int]  ,__UpperCamelCase							:	list[list[int]]  ,__UpperCamelCase							:	int  ,__UpperCamelCase							:	dict[int, list[int]]  ,__UpperCamelCase							:	int = 1  ,):
		lowerCAmelCase_    :  Optional[Any]             =				0
		lowerCAmelCase_    :  Queue[int]             =				Queue(maxsize=UpperCamelCase__				)
		q.put(UpperCamelCase__				)
		while q.qsize() != 0:
				lowerCAmelCase_    :  Tuple             =				q.get()
				for v in graph[u]:
						if level[v] == -1:
								lowerCAmelCase_    :  List[str]             =				level[u] + 1
								q.put(UpperCamelCase__				)
								lowerCAmelCase_    :  Optional[Any]             =				u
		return level, parent


def     UpperCamelCase(						):
		lowerCAmelCase_    :  Tuple             =				13
		# initializing with 0
		lowerCAmelCase_    :  Optional[Any]             =				[[0 for _ in range(max_node + 10				)] for _ in range(20				)]
		# initializing with -1 which means every node is unvisited
		lowerCAmelCase_    :  Any             =				[-1 for _ in range(max_node + 10				)]
		lowerCAmelCase_    :  dict[int, list[int]]             =				{
		    1: [2, 3, 4],
		    2: [5],
		    3: [6, 7],
		    4: [8],
		    5: [9, 10],
		    6: [11],
		    7: [],
		    8: [12, 13],
		    9: [],
		    10: [],
		    11: [],
		    12: [],
		    13: [],
		}
		lowerCAmelCase_    :  Optional[int]             =				breadth_first_search(UpperCamelCase__  ,UpperCamelCase__  ,UpperCamelCase__  ,UpperCamelCase__  ,1				)
		lowerCAmelCase_    :  Dict             =				create_sparse(UpperCamelCase__  ,UpperCamelCase__				)
		print('''LCA of node 1 and 3 is: '''  ,lowest_common_ancestor(1  ,3  ,UpperCamelCase__  ,UpperCamelCase__				)				)
		print('''LCA of node 5 and 6 is: '''  ,lowest_common_ancestor(5  ,6  ,UpperCamelCase__  ,UpperCamelCase__				)				)
		print('''LCA of node 7 and 11 is: '''  ,lowest_common_ancestor(7  ,11  ,UpperCamelCase__  ,UpperCamelCase__				)				)
		print('''LCA of node 6 and 7 is: '''  ,lowest_common_ancestor(6  ,7  ,UpperCamelCase__  ,UpperCamelCase__				)				)
		print('''LCA of node 4 and 12 is: '''  ,lowest_common_ancestor(4  ,12  ,UpperCamelCase__  ,UpperCamelCase__				)				)
		print('''LCA of node 8 and 8 is: '''  ,lowest_common_ancestor(8  ,8  ,UpperCamelCase__  ,UpperCamelCase__				)				)


if __name__ == "__main__":
						main()










from __future__ import annotations

from typing import Generic, TypeVar

A__						:						Dict          =      TypeVar('''T''')



class        __snake_case		(  Generic[T]		):




							def __init__( self  : List[Any]       ,       A_  : T):
									lowerCAmelCase_    :  Optional[Any]             =				data  # Assign data
									lowerCAmelCase_    :  Node[T] | None             =				None  # Initialize next as null
									lowerCAmelCase_    :  Node[T] | None             =				None  # Initialize prev as null





class        __snake_case		(  Generic[T]		):




							def __init__( self  : Union[str, Any]):
									lowerCAmelCase_    :  Node[T] | None             =				None




							def      UpperCAmelCase__    ( self  : List[str]       ,       A_  : T):
									if self.head is None:
											lowerCAmelCase_    :  List[Any]             =				Node(A_)
									else:
											lowerCAmelCase_    :  Tuple             =				Node(A_)
											lowerCAmelCase_    :  Optional[int]             =				new_node
											lowerCAmelCase_    :  Optional[int]             =				self.head
											lowerCAmelCase_    :  Optional[Any]             =				None
											lowerCAmelCase_    :  Dict             =				new_node




							def      UpperCAmelCase__    ( self  : List[str]):
									if self.head is None:
											return None
									else:
											assert self.head is not None
											lowerCAmelCase_    :  Tuple             =				self.head.data
											lowerCAmelCase_    :  Any             =				self.head.next
											if self.head is not None:
													lowerCAmelCase_    :  Optional[Any]             =				None
											return temp




							def      UpperCAmelCase__    ( self  : Union[str, Any]):
									return self.head.data if self.head is not None else None




							def __len__( self  : Dict):
									lowerCAmelCase_    :  List[str]             =				self.head
									lowerCAmelCase_    :  str             =				0
									while temp is not None:
											count += 1
											lowerCAmelCase_    :  Union[str, Any]             =				temp.next
									return count




							def      UpperCAmelCase__    ( self  : Dict):
									return self.head is None







							def      UpperCAmelCase__    ( self  : Dict):
									print('''stack elements are:''')
									lowerCAmelCase_    :  Optional[Any]             =				self.head
									while temp is not None:
											print(temp.data       ,       end='''->''')
											lowerCAmelCase_    :  List[str]             =				temp.next


# Code execution starts here
if __name__ == "__main__":
						# Start with the empty stack
						A__						:						Stack[int]          =      Stack()

						# Insert 4 at the beginning. So stack becomes 4->None
						print('''Stack operations using Doubly LinkedList''')
						stack.push(4)

						# Insert 5 at the beginning. So stack becomes 4->5->None
						stack.push(5)

						# Insert 6 at the beginning. So stack becomes 4->5->6->None
						stack.push(6)

						# Insert 7 at the beginning. So stack becomes 4->5->6->7->None
						stack.push(7)

						# Print the stack
						stack.print_stack()

						# Print the top element
						print('''\nTop element is ''', stack.top())

						# Print the stack size
						print('''Size of the stack is ''', len(stack))

						# pop the top element
						stack.pop()

						# pop the top element
						stack.pop()

						# two elements have now been popped off
						stack.print_stack()

						# Print True if the stack is empty else False
						print('''\nstack is empty:''', stack.is_empty())





