No.1647 Travel in Mitaru city 2
Problem Statement
Mitaru City is a town that can be represented as an $H \times W$ grid. The cell in the $i$-th row from the top and the $j$-th column from the left is denoted by $(i,j)$.
Mitaru City has $N$ tourist attractions, and the $i$-th tourist attraction is located at $(x_i,y_i)$.
A sequence $T$ of positive integers of length $X$ is called a good journey if it satisfies the following conditions. Here, $T_i$ denotes the $i$-th term of $T$, and $T_{X+1}$ is regarded as $T_1$.
- $X \geq 4$
- $1 \leq T_i \leq N$ $(\ 1 \leq i \leq X\ )$
- $T_i \neq T_j$ $(\ i \neq j\ )$
- For every integer $i$ satisfying $1 \leq i \leq X$, the following holds.
- If $i$ is even, the $T_i$-th tourist attraction and the $T_{i+1}$-th tourist attraction are in the same row. That is, $x_{T_i}=x_{T_{i+1}}$ holds.
- If $i$ is odd, the $T_i$-th tourist attraction and the $T_{i+1}$-th tourist attraction are in the same column. That is, $y_{T_i}=y_{T_{i+1}}$ holds.
Determine whether a good journey exists, and if it does, show $1$ of them.
Input
$H\ \ W\ \ N$
$x_1\ \ y_1$
$x_2\ \ y_2$
$\vdots$
$x_N\ \ y_N$
- $2 \leq H,W \leq 10^5$
- $4 \leq N \leq \mathrm{min}(H \times W,10^5)$
- $1 \leq x_i \leq H$ ( $1 \leq i \leq N$ )
- $1 \leq y_i \leq W$ ( $1 \leq i \leq N$ )
- $(x_i,y_i)$ are pairwise distinct
- All inputs are integers
Output
If no good journey exists, output -1 followed by a newline.
If one exists, output $1$ of them in the following format. If there are multiple solutions, you may output any of them.
$X$
$T_1\ \ T_2\ \ \cdots T_X$
Sample
Sample 1
Input
2 2 4
1 1
1 2
2 1
2 2
Output
4
1 3 4 2
Besides this, the following output is also accepted.
4
4 2 1 3
Sample 2
Input
3 3 4
1 1
2 2
3 3
1 3
Output
-1
Sample 3
Input
4 4 8
1 1
3 1
3 3
2 3
2 2
4 2
4 4
1 4
Output
8
1 2 3 4 5 6 7 8
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
蜜蜂