#P10760. [BalticOI 2024] Portal
[BalticOI 2024] Portal
Background
Translated from BalticOI 2024 Day1 T2.
Problem Description
You are playing a prank on your friend. Your friend starts at position . Each grid cell has a color. There are portals. When your friend enters a portal cell, they will be teleported uniformly at random to one of the portals (possibly the same portal; if there is a portal at , they must also be teleported).
Unfortunately, your friend does not know that portals exist. They can only determine their current cell and its color based on the path they have walked. You need to ensure that no matter how they move, the color of the cell they see is always the same as the color of the cell they believe they are in.
You may color the grid arbitrarily, but you must maximize the number of colors used. It is not hard to see that coloring everything with one color is always the smallest solution.
Input Format
The first line contains an integer , meaning there are portals.
The next lines each contain two integers , representing the coordinates of a portal.
Output Format
Output one number, the maximum number of colors you can use. If you can use infinitely many colors, output .
3
1 1
1 3
3 2
4
5
0 0
1 0
-1 0
0 1
0 -1
1
1
1 -1
-1
Hint
| Subtask ID | Special Property | Score |
|---|---|---|
| For all portal cells and , it is guaranteed that is also a portal cell | ||
| and | ||
| No special properties |
For all testdata, , , and all are pairwise distinct.
For Sample 1, the portals are placed at , , and . If your friend moves in the following order: up, right, down, left.

After this sequence of moves ends, your friend will believe they have returned to the starting cell , but in reality they may also end up at or . They have already seen the color of cell at the start, so if they now see a different color, they will realize that portals must exist. We do not want this to happen, so we must choose the same color for these three cells.
There is no sequence of moves that makes your friend believe they finally end up in cell while actually ending up in , so these cells can safely use different colors.
You can see a coloring scheme using colors in the example below. For this example, it is impossible to use more than colors.

For Sample 2, let us consider a different example, where there are portals at cells , , , , and . Suppose your friend tries to reach cell by first moving right once and then moving up three times. One possibility is that if they are teleported there at the start and after every step, then they will finally reach cell . If your friend now returns to what they believe is cell by moving down three times and then left once, and during this process they are not teleported away from their current cell, then they will finally arrive at . Your friend will believe for the second time that they are at cell , and expect to see the same color. So you need to color and with the same color.
Note that there is nothing special about choosing cell as the starting point. You can also prove that other cells must share the same color as .
Translated by ChatGPT 5