


"""simple docstring"""


from abc import ABC, abstractmethod
from typing import List, Optional
class     _UpperCamelCase    (  lowerCAmelCase__						):
							'''simple docstring'''







							def __init__(	self      ):
										# test for the above condition
										self.test()

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     0
										__lowerCAmelCase							       =     False
										while not completed:
													if counter == 1:
																self.reset()
													__lowerCAmelCase							       =     self.advance()
													if not self.does_advance(__a      ):
																raise Exception(
																    "Custom Constraint is not defined correctly. self.does_advance(self.advance()) must be true."      )

													__lowerCAmelCase       ,      __lowerCAmelCase       ,      __lowerCAmelCase							       =     self.update(__a      )
													counter += 1

													if counter > 1_00_00:
																raise Exception("update() does not fulfill the constraint."      )

										if self.remaining() != 0:
													raise Exception("Custom Constraint is not defined correctly."      )

							@abstractmethod
							def      snake_case							(	self      ):
										raise NotImplementedError(
										    f"{self.__class__} is an abstract class. Only classes inheriting this class can be called."      )

							@abstractmethod
							def      snake_case							(	self       ,  __a      ):
										raise NotImplementedError(
										    f"{self.__class__} is an abstract class. Only classes inheriting this class can be called."      )

							@abstractmethod
							def      snake_case							(	self       ,  __a      ):
										raise NotImplementedError(
										    f"{self.__class__} is an abstract class. Only classes inheriting this class can be called."      )

							@abstractmethod
							def      snake_case							(	self      ):
										raise NotImplementedError(
										    f"{self.__class__} is an abstract class. Only classes inheriting this class can be called."      )

							@abstractmethod
							def      snake_case							(	self      ):
										raise NotImplementedError(
										    f"{self.__class__} is an abstract class. Only classes inheriting this class can be called."      )



							@abstractmethod
							def      snake_case							(	self       ,  __a=False      ):
										raise NotImplementedError(
										    f"{self.__class__} is an abstract class. Only classes inheriting this class can be called."      )
class     _UpperCamelCase    (  lowerCAmelCase__						):
							'''simple docstring'''







							def __init__(	self       ,  __a      ):
										super(__a       ,  self      ).__init__()

										if not isinstance(__a       ,  __a      ) or len(__a      ) == 0:
													raise ValueError(f"`token_ids` has to be a non-empty list, but is {token_ids}."      )
										if any((not isinstance(__a       ,  __a      ) or token_id < 0) for token_id in token_ids      ):
													raise ValueError(f"Each list in `token_ids` has to be a list of positive integers, but is {token_ids}."      )

										__lowerCAmelCase							       =     token_ids

										__lowerCAmelCase							       =     len(self.token_ids      )
										__lowerCAmelCase							       =     -1  # the index of the currently fulfilled step
										__lowerCAmelCase							       =     False

							def      snake_case							(	self      ):
										if self.completed:
													return None
										return self.token_ids[self.fulfilled_idx + 1]

							def      snake_case							(	self       ,  __a      ):
										if not isinstance(__a       ,  __a      ):
													raise ValueError(f"`token_id` has to be an `int`, but is {token_id} of type {type(__a      )}"      )

										if self.completed:
													return False

										return token_id == self.token_ids[self.fulfilled_idx + 1]

							def      snake_case							(	self       ,  __a      ):
										if not isinstance(__a       ,  __a      ):
													raise ValueError(f"`token_id` has to be an `int`, but is {token_id} of type {type(__a      )}"      )

										__lowerCAmelCase							       =     False
										__lowerCAmelCase							       =     False
										__lowerCAmelCase							       =     False

										if self.does_advance(__a      ):
													self.fulfilled_idx += 1
													__lowerCAmelCase							       =     True
													if self.fulfilled_idx == (self.seqlen - 1):
																__lowerCAmelCase							       =     True
													__lowerCAmelCase							       =     completed
										else:
													# failed to make progress.
													__lowerCAmelCase							       =     True
													self.reset()
										return stepped, completed, reset

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     False
										__lowerCAmelCase							       =     0

							def      snake_case							(	self      ):
										return self.seqlen - (self.fulfilled_idx + 1)



							def      snake_case							(	self       ,  __a=False      ):
										__lowerCAmelCase							       =     PhrasalConstraint(self.token_ids      )

										if stateful:
													__lowerCAmelCase							       =     self.seqlen
													__lowerCAmelCase							       =     self.fulfilled_idx
													__lowerCAmelCase							       =     self.completed

										return new_constraint
class     _UpperCamelCase    :
							'''simple docstring'''







							def __init__(	self       ,  __a       ,  __a=True      ):
										__lowerCAmelCase							       =     max([len(__a      ) for one in nested_token_ids]      )

										__lowerCAmelCase							       =     {}
										for token_ids in nested_token_ids:
													__lowerCAmelCase							       =     root
													for tidx, token_id in enumerate(__a      ):
																if token_id not in level:
																			__lowerCAmelCase							       =     {}

																__lowerCAmelCase							       =     level[token_id]

										if no_subsets and self.has_subsets(__a       ,  __a      ):
													raise ValueError(
													    "Each list in `nested_token_ids` can't be a complete subset of another list, but is"
													    f" {nested_token_ids}."      )

										__lowerCAmelCase							       =     root

							def      snake_case							(	self       ,  __a      ):
										__lowerCAmelCase							       =     self.trie

										for current_token in current_seq:
													__lowerCAmelCase							       =     start[current_token]

										__lowerCAmelCase							       =     list(start.keys()      )

										return next_tokens

							def      snake_case							(	self       ,  __a      ):
										__lowerCAmelCase							       =     self.next_tokens(__a      )

										return len(__a      ) == 0

							def      snake_case							(	self       ,  __a      ):
										__lowerCAmelCase							       =     list(root.values()      )
										if len(__a      ) == 0:
													return 1
										else:
													return sum([self.count_leaves(__a      ) for nn in next_nodes]      )



							def      snake_case							(	self       ,  __a       ,  __a      ):
										__lowerCAmelCase							       =     self.count_leaves(__a      )
										return len(__a      ) != leaf_count
class     _UpperCamelCase    (  lowerCAmelCase__						):
							'''simple docstring'''







							def __init__(	self       ,  __a      ):
										super(__a       ,  self      ).__init__()

										if not isinstance(__a       ,  __a      ) or len(__a      ) == 0:
													raise ValueError(f"`nested_token_ids` has to be a non-empty list, but is {nested_token_ids}."      )
										if any(not isinstance(__a       ,  __a      ) for token_ids in nested_token_ids      ):
													raise ValueError(f"`nested_token_ids` has to be a list of lists, but is {nested_token_ids}."      )
										if any(
										    any((not isinstance(__a       ,  __a      ) or token_id < 0) for token_id in token_ids      )
										    for token_ids in nested_token_ids      ):
													raise ValueError(
													    f"Each list in `nested_token_ids` has to be a list of positive integers, but is {nested_token_ids}."      )

										__lowerCAmelCase							       =     DisjunctiveTrie(__a      )
										__lowerCAmelCase							       =     nested_token_ids

										__lowerCAmelCase							       =     self.trie.max_height
										__lowerCAmelCase							       =     []
										__lowerCAmelCase							       =     False

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     self.trie.next_tokens(self.current_seq      )

										if len(__a      ) == 0:
													return None
										else:
													return token_list

							def      snake_case							(	self       ,  __a      ):
										if not isinstance(__a       ,  __a      ):
													raise ValueError(f"`token_id` is supposed to be type `int`, but is {token_id} of type {type(__a      )}"      )

										__lowerCAmelCase							       =     self.trie.next_tokens(self.current_seq      )

										return token_id in next_tokens

							def      snake_case							(	self       ,  __a      ):
										if not isinstance(__a       ,  __a      ):
													raise ValueError(f"`token_id` is supposed to be type `int`, but is {token_id} of type {type(__a      )}"      )

										__lowerCAmelCase							       =     False
										__lowerCAmelCase							       =     False
										__lowerCAmelCase							       =     False

										if self.does_advance(__a      ):
													self.current_seq.append(__a      )
													__lowerCAmelCase							       =     True
										else:
													__lowerCAmelCase							       =     True
													self.reset()

										__lowerCAmelCase							       =     self.trie.reached_leaf(self.current_seq      )
										__lowerCAmelCase							       =     completed

										return stepped, completed, reset

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     False
										__lowerCAmelCase							       =     []

							def      snake_case							(	self      ):
										if self.completed:
													# since this can be completed without reaching max height
													return 0
										else:
													return self.seqlen - len(self.current_seq      )



							def      snake_case							(	self       ,  __a=False      ):
										__lowerCAmelCase							       =     DisjunctiveConstraint(self.token_ids      )

										if stateful:
													__lowerCAmelCase							       =     self.seqlen
													__lowerCAmelCase							       =     self.current_seq
													__lowerCAmelCase							       =     self.completed

										return new_constraint
class     _UpperCamelCase    :
							'''simple docstring'''







							def __init__(	self       ,  __a      ):
										__lowerCAmelCase							       =     constraints

										# max # of steps required to fulfill a given constraint
										__lowerCAmelCase							       =     max([c.seqlen for c in constraints]      )
										__lowerCAmelCase							       =     len(__a      )
										__lowerCAmelCase							       =     False

										self.init_state()

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     []
										__lowerCAmelCase							       =     None
										__lowerCAmelCase							       =     [constraint.copy(stateful=__a      ) for constraint in self.constraints]

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     0
										if self.inprogress_constraint:
													# extra points for having a constraint mid-fulfilled
													add += self.max_seqlen - self.inprogress_constraint.remaining()

										return (len(self.complete_constraints      ) * self.max_seqlen) + add

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     []
										if self.inprogress_constraint is None:
													for constraint in self.pending_constraints:  # "pending" == "unfulfilled yet"
																__lowerCAmelCase							       =     constraint.advance()
																if isinstance(__a       ,  __a      ):
																			token_list.append(__a      )
																elif isinstance(__a       ,  __a      ):
																			token_list.extend(__a      )
										else:
													__lowerCAmelCase							       =     self.inprogress_constraint.advance()
													if isinstance(__a       ,  __a      ):
																token_list.append(__a      )
													elif isinstance(__a       ,  __a      ):
																token_list.extend(__a      )

										if len(__a      ) == 0:
													return None
										else:
													return token_list

							def      snake_case							(	self       ,  __a      ):
										self.init_state()

										if token_ids is not None:
													for token in token_ids:
																# completes or steps **one** constraint
																__lowerCAmelCase       ,      __lowerCAmelCase							       =     self.add(__a      )

																# the entire list of constraints are fulfilled
																if self.completed:
																			break

							def      snake_case							(	self       ,  __a      ):
										if not isinstance(__a       ,  __a      ):
													raise ValueError(f"`token_id` should be an `int`, but is `{token_id}`."      )

										__lowerCAmelCase       ,      __lowerCAmelCase							       =     False, False

										if self.completed:
													__lowerCAmelCase							       =     True
													__lowerCAmelCase							       =     False
													return complete, stepped

										if self.inprogress_constraint is not None:
													# In the middle of fulfilling a constraint. If the `token_id` *does* makes an incremental progress to current
													# job, simply update the state

													__lowerCAmelCase       ,      __lowerCAmelCase       ,      __lowerCAmelCase							       =     self.inprogress_constraint.update(__a      )
													if reset:
																# 1. If the next token breaks the progress, then we must restart.
																#     e.g. constraint = "I love pies" and sequence so far is "I love" but `token_id` == "books".

																#     But that doesn't mean we self.init_state(), since we only reset the state for this particular
																#     constraint, not the full list of constraints.

																self.pending_constraints.append(self.inprogress_constraint.copy(stateful=__a      )      )
																__lowerCAmelCase							       =     None

													if complete:
																# 2. If the next token completes the constraint, move it to completed list, set
																#     inprogress to None. If there are no pending constraints either, then this full list of constraints
																#     is complete.

																self.complete_constraints.append(self.inprogress_constraint      )
																__lowerCAmelCase							       =     None

																if len(self.pending_constraints      ) == 0:
																			# we're done!
																			__lowerCAmelCase							       =     True

										else:
													# Not in the middle of fulfilling a constraint. So does this `token_id` helps us step towards any of our list
													# of constraints?

													for cidx, pending_constraint in enumerate(self.pending_constraints      ):
																if pending_constraint.does_advance(__a      ):
																			__lowerCAmelCase       ,      __lowerCAmelCase       ,      __lowerCAmelCase							       =     pending_constraint.update(__a      )

																			if not stepped:
																						raise Exception(
																						    "`constraint.update(token_id)` is not yielding incremental progress, "
																						    "even though `constraint.does_advance(token_id)` is true."      )

																			if complete:
																						self.complete_constraints.append(__a      )
																						__lowerCAmelCase							       =     None

																			if not complete and stepped:
																						__lowerCAmelCase							       =     pending_constraint

																			if complete or stepped:
																						# If we made any progress at all, then it's at least not a "pending constraint".

																						__lowerCAmelCase							       =     (
																						    self.pending_constraints[:cidx] + self.pending_constraints[cidx + 1 :]
																						)

																						if len(self.pending_constraints      ) == 0 and self.inprogress_constraint is None:
																									# If there's no longer any pending after this and no inprogress either, then we must be
																									# complete.

																									__lowerCAmelCase							       =     True

																						break  # prevent accidentally stepping through multiple constraints with just one token.

										return complete, stepped



							def      snake_case							(	self       ,  __a=True      ):
										__lowerCAmelCase							       =     ConstraintListState(self.constraints      )  # we actually never though self.constraints objects
										# throughout this process. So it's at initialization state.

										if stateful:
													__lowerCAmelCase							       =     [
													    constraint.copy(stateful=__a      ) for constraint in self.complete_constraints
													]
													if self.inprogress_constraint is not None:
																__lowerCAmelCase							       =     self.inprogress_constraint.copy(stateful=__a      )
													__lowerCAmelCase							       =     [constraint.copy() for constraint in self.pending_constraints]

										return new_state








"""simple docstring"""


def _lowerCamelCase      (						_UpperCamelCase       ,					_UpperCamelCase       ,					_UpperCamelCase       ,					_UpperCamelCase ):

			'''simple docstring'''




			__lowerCAmelCase							       =     [False] * len(_UpperCamelCase )
			__lowerCAmelCase							       =     []
			queue.append(_UpperCamelCase )
			__lowerCAmelCase							       =     True

			while queue:
						__lowerCAmelCase							       =     queue.pop(0 )
						for ind in range(len(graph[u] ) ):
									if visited[ind] is False and graph[u][ind] > 0:
												queue.append(_UpperCamelCase )
												__lowerCAmelCase							       =     True
												__lowerCAmelCase							       =     u

			return visited[t]


def _lowerCamelCase      (						_UpperCamelCase       ,					_UpperCamelCase       ,					_UpperCamelCase ):

			'''simple docstring'''




			__lowerCAmelCase							       =     [-1] * (len(_UpperCamelCase ))
			__lowerCAmelCase							       =     0
			while bfs(_UpperCamelCase       ,					_UpperCamelCase       ,					_UpperCamelCase       ,					_UpperCamelCase ):
						__lowerCAmelCase							       =     float("Inf" )
						__lowerCAmelCase							       =     sink

						while s != source:
									# Find the minimum value in select path
									__lowerCAmelCase							       =     min(_UpperCamelCase       ,					graph[parent[s]][s] )
									__lowerCAmelCase							       =     parent[s]

						max_flow += path_flow
						__lowerCAmelCase							       =     sink

						while v != source:
									__lowerCAmelCase							       =     parent[v]
									graph[u][v] -= path_flow
									graph[v][u] += path_flow
									__lowerCAmelCase							       =     parent[v]
			return max_flow


A	:  Union[str, Any]            =     [
    [0, 1_6, 1_3, 0, 0, 0],
    [0, 0, 1_0, 1_2, 0, 0],
    [0, 4, 0, 0, 1_4, 0],
    [0, 0, 9, 0, 0, 2_0],
    [0, 0, 0, 7, 0, 4],
    [0, 0, 0, 0, 0, 0],
]

A       ,       A	:  Any            =     0, 5


print(ford_fulkerson(graph, source, sink))




