#C1820. Minimum Route Changes to Avoid Overlap

    ID: 45068 Type: Default 1000ms 256MiB

Minimum Route Changes to Avoid Overlap

Minimum Route Changes to Avoid Overlap

In this problem, you are given an integer ( n ) and two lists of integers, ( A ) and ( B ), each of length ( n ), representing the station routes of two trains. The goal is to ensure that the two routes have no station in common by changing the overlapping stations. A change means modifying the station number in one of the routes to any number that does not cause duplicates in that route and does not conflict with the other route.

You should note that if any of the routes contains duplicates initially (i.e. the list is not a permutation), then it is impossible to perform the changes and the output should be the string Impossible.

Otherwise, the minimum number of changes required is equal to the number of overlapping stations, i.e. ( |A \cap B| ). Your task is to compute and output this number, or output Impossible if the operation cannot be performed.

inputFormat

The input is given via standard input (stdin) in the following format:

The first line contains an integer ( n ), the number of stations in each route.
The second line contains ( n ) space-separated integers representing the route of train A.
The third line contains ( n ) space-separated integers representing the route of train B.

outputFormat

Output a single line to standard output (stdout):

  • If either route A or route B contains duplicates, output the string Impossible (without quotes).
  • Otherwise, output an integer representing the minimum number of changes required, which is the number of common stations between the two routes, i.e. ( |A \cap B| ).## sample
3
1 2 3
4 5 6
0