問題一覧 > 通常問題

No.1647 Travel in Mitaru city 2

レベル : / AI推定難易度 : 1725 / 実行時間制限 : 1ケース 2.500秒 / メモリ制限 : 512 MB / スペシャルジャッジ問題 (複数の解が存在する可能性があります)
タグ : (AC するまで非表示) / 解いたユーザー数 65
作問者 : 蜜蜂 / テスター : Mitarushi ygussany
お気に入りにしたユーザー ProblemId : 6788 / 自分の提出
問題文最終更新日: 2021-08-13 23:56:30
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 309 (順位表) の他の問題:

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もしくは右上の雲マークをクリックしてアカウントを作成してください。