#C8515. Minimum Bus Route Changes

    ID: 52506 Type: Default 1000ms 256MiB

Minimum Bus Route Changes

Minimum Bus Route Changes

You are given a grid with N rows and M columns. There are R bus routes operating in the grid. Each bus route is described by a sequence of stops (grid intersections). A passenger may ride a bus along its route without making any transfers. However, if two bus routes share a common stop, a passenger can transfer from one route to the other at that stop.

Assume that a journey between two grid points is possible only if they are connected through one or more bus routes. The minimum number of bus route changes required for a journey is defined as follows: if a passenger can complete the journey using a single bus route, no transfer is needed (answer 0); if the journey necessarily involves switching from one bus to another – that is, there is at least one bus stop served by at least two routes – then the answer is 1.

Your task is to determine whether any transfer is possible in the bus network. Print 1 if there exists at least one stop that is served by two or more routes (i.e. a transfer is possible), or 0 if no transfers are possible (i.e. all routes are isolated).

inputFormat

The input is provided via standard input and has the following format:

The first line contains two integers N and M, representing the number of rows and columns of the grid. The second line contains an integer R, the number of bus routes. Each of the following R lines describes a bus route. A route description starts with an integer k (the number of stops in that route) followed by 2*k integers. These integers represent the row and column indices (in that order) of the stops along the route.

All numbers are separated by spaces and/or newlines.

outputFormat

Output a single integer to standard output. The integer should be 1 if there exists at least one bus stop that is shared by two or more routes (making a transfer possible), or 0 if each route is completely isolated.## sample

5 5
4
3 2 3 2 4 2 5
3 1 3 2 3 3 3
3 3 3 4 3 5 3
3 4 1 4 2 4 3
1