


from __future__ import annotations

from queue import Queue







def 	A (		A__		,		A__			):
     a ^= b
     b ^= a
     a ^= b
     return a, b







def 	A (		A__		,		A__			):
     UpperCAmelCase    :					List[str]			=      1
     while (1 << j) < max_node:
          for i in range(1		,		max_node + 1			):
               UpperCAmelCase    :					List[Any]			=      parent[j - 1][parent[j - 1][i]]
          j += 1
     return parent







def 	A (		A__		,		A__		,		A__		,		A__			):
     # u must be deeper in the tree than v
     if level[u] < level[v]:
          UpperCAmelCase	,			UpperCAmelCase    :					Optional[int]			=      swap(A__		,		A__			)
     # making depth of u same as depth of v
     for i in range(1_8		,		-1		,		-1			):
          if level[u] - (1 << i) >= level[v]:
               UpperCAmelCase    :					str			=      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(1_8		,		-1		,		-1			):
          if parent[i][u] not in [0, parent[i][v]]:
               UpperCAmelCase	,			UpperCAmelCase    :					int			=      parent[i][u], parent[i][v]
    # returning longest common ancestor of u,v
     return parent[0][u]







def 	A (		A__		,		A__		,		A__		,		A__		,		A__ = 1		,		):
     UpperCAmelCase    :					int			=      0
     UpperCAmelCase    :					Queue[int]			=      Queue(maxsize=A__			)
     q.put(A__			)
     while q.qsize() != 0:
          UpperCAmelCase    :					List[Any]			=      q.get()
          for v in graph[u]:
               if level[v] == -1:
                    UpperCAmelCase    :					int			=      level[u] + 1
                    q.put(A__			)
                    UpperCAmelCase    :					Union[str, Any]			=      u
     return level, parent







def 	A (		):
     UpperCAmelCase    :					List[str]			=      1_3
     # initializing with 0
     UpperCAmelCase    :					Tuple			=      [[0 for _ in range(max_node + 1_0			)] for _ in range(2_0			)]
     # initializing with -1 which means every node is unvisited
     UpperCAmelCase    :					str			=      [-1 for _ in range(max_node + 1_0			)]
     UpperCAmelCase    :					dict[int, list[int]]			=      {
         1: [2, 3, 4],
         2: [5],
         3: [6, 7],
         4: [8],
         5: [9, 1_0],
         6: [1_1],
         7: [],
         8: [1_2, 1_3],
         9: [],
         1_0: [],
         1_1: [],
         1_2: [],
         1_3: [],
     }
     UpperCAmelCase	,			UpperCAmelCase    :					List[Any]			=      breadth_first_search(A__		,		A__		,		A__		,		A__		,		1			)
     UpperCAmelCase    :					Optional[int]			=      create_sparse(A__		,		A__			)
     print("LCA of node 1 and 3 is: "		,		lowest_common_ancestor(1		,		3		,		A__		,		A__			)			)
     print("LCA of node 5 and 6 is: "		,		lowest_common_ancestor(5		,		6		,		A__		,		A__			)			)
     print("LCA of node 7 and 11 is: "		,		lowest_common_ancestor(7		,		1_1		,		A__		,		A__			)			)
     print("LCA of node 6 and 7 is: "		,		lowest_common_ancestor(6		,		7		,		A__		,		A__			)			)
     print("LCA of node 4 and 12 is: "		,		lowest_common_ancestor(4		,		1_2		,		A__		,		A__			)			)
     print("LCA of node 8 and 8 is: "		,		lowest_common_ancestor(8		,		8		,		A__		,		A__			)			)


if __name__ == "__main__":
      main()







from __future__ import annotations







def 	A (		A__		,		A__		,		A__		,		A__			):

     # Base Case
     if counter == 0 or capacity == 0:
          return 0

     # If weight of the nth item is more than Knapsack of capacity,
     #   then this item cannot be included in the optimal solution,
     # else return the maximum of two cases:
     #   (1) nth item included
     #   (2) not included
     if weights[counter - 1] > capacity:
          return knapsack(A__		,		A__		,		A__		,		counter - 1			)
     else:
          UpperCAmelCase    :					List[str]			=      capacity - weights[counter - 1]
          UpperCAmelCase    :					Tuple			=      values[counter - 1] + knapsack(
              A__		,		A__		,		A__		,		counter - 1			)
          UpperCAmelCase    :					Optional[int]			=      knapsack(A__		,		A__		,		A__		,		counter - 1			)
          return max(A__		,		A__			)


if __name__ == "__main__":
      import doctest

      doctest.testmod()



