COCI '21 Contest 5 #5 Usmjeravanje

View as PDF

Submit solution


Points: 20 (partial)
Time limit: 1.0s
Memory limit: 512M

Problem types

Peter Pan was given career guidance to help determine his future profession. As he does not want to grow up, he ran away and sought shelter in Neverland. There are two rivers in Neverland, flowing from west to east. On the shore of the first river, there are a cities, labeled with positive integers from 1 to a in the same direction that the river flows. Similarly, on the shore of the second river there are b cities labeled in the same direction from 1 to b. Traveling downstream, it's possible to reach city j from city i if both of these cities are on the same river and if i < j.

Citizens of Neverland plan to establish m one-way flight routes. It is given that the i^\text{th} route should connect city x_i from the first river and city y_i from the second river, but it has not yet been decided in which direction. The citizens of Neverland would like their cities to be as connected as possible. At that moment Peter Pan realized that he would like to direct flight routes for a living.

A pair of cities is called connected if it is possible to reach the second city starting from the first, and vice versa. While traveling, it is allowed to use both flight routes and rivers. Peter Pan wants to determine the route directions in order to minimize the largest set of cities in which no pair of cities is connected. Help Peter Pan and determine how to direct the routes and what would the size of the mentioned set be in that case.

Input Specification

The first line contains positive integers a, b, and m (1 \le a, b, m \le 200\,000), the number of cities on the first river, the number of cities on the second river and the number of flight routes, respectively.

The i^\text{th} of the next m lines contains two positive integers x_i and y_i (1 \le x_i \le a, 1 \le y_i \le b) which denote a flight route connecting city x_i from the first river and city y_i from the second river. No pair of cities is listed more than once.

Output Specification

In the first line print the least possible size of the maximum set of cities in which no pair of cities is connected.

In the second line print a sequence of characters 0 or 1 separated by spaces which denote the directions of the flight routes. The character 0 means that the flight takes off from the first river and lands on the second river, and conversely for 1. If there is more than one solution, output any.

Constraints

SubtaskPointsConstraints
1201 \le a, b, m \le 15
2301 \le a, b \le 1\,000
360No additional constraints.

Sample Input 1

5 3
4
1 2
2 3
3 1
5 3

Sample Output 1

1
1 1 0 0

Explanation for Sample Output 1

If the flights are directed as shown in the output, it is possible to reach any city starting from any other city. Therefore, the largest set containing no pair of connected cities is a singleton. For example, it's possible to reach city 1 from the first river by starting from city 5 from the first river:

\displaystyle 5 (I) \to 3 (II) \to 2 (I) \to 3 (I) \to 1 (II) \to 2 (II) \to 1 (I)

Sample Input 2

6 6
4
1 2
3 2
4 3
5 6

Sample Output 2

9
1 0 1 1

Sample Input 3

8 7
7
1 3
2 1
3 4
5 6
6 5
6 7
8 7

Sample Output 3

5
1 0 1 1 0 1 0

Comments

There are no comments at the moment.