QOJ.ac

QOJ

Limite de temps : 1 s Limite de mémoire : 2048 MB Points totaux : 100 Difficulté: [afficher] Hackable ✓

#18202. DFS Order 6

Statistiques

Little Cyan Fish は DFS 順序が大好きです。今日、彼はそれを再び研究していますが、今回は根付き木ではなく、無向単純グラフを対象としています。

頂点 $1$ から $n$ までラベル付けされた連結な無向単純グラフ $G$ と、開始頂点 $s$ を固定します。$G$ の $s$ からの DFS 順序とは、以下の深さ優先探索によって頂点が最初に訪問される順序のことです。同順位の場合は、常にラベルが最小の未訪問の隣接頂点へ進むことで解消されるため、DFS 順序は一意に定まります。

アルゴリズム1 本問題で用いられるDFS順

 1: procedure DFS(vertex x)
 2:     Mark x as visited
 3:     Append x to the end of dfs_order
 4:     for each vertex y adjacent to x in G, in ascending order of label do
 5:         if y is not yet visited then
 6:             DFS(y)
 7:         end if
 8:     end for
 9: end procedure
10:
11: procedure GENERATE(G, vertex s)
12:     Mark all vertices as unvisited
13:     Let dfs_order be an empty list
14:     DFS(s)
15:     return dfs_order
16: end procedure
problem_18202_1.png

7つの頂点と7つの辺を持つグラフ。頂点 1 からの DFS 順序は [1, 2, 3, 7, 4, 5, 6] です。

Little Cyan Fish は $1$ から $n$ までの $n$ 個の順列 $a_1, a_2, \dots, a_n$ を用意しました。各 $a_i = [a_{i,1}, a_{i,2}, \dots, a_{i,n}]$ は、彼が主張する頂点 $i$ からの DFS 順序です。

すべての開始頂点 $i$ からの DFS 順序が $a_i$ と一致するような、頂点 $1, 2, \dots, n$ 上の連結な無向単純グラフ $G$ を再構築してください。そのようなグラフが存在しない場合は、その旨を判定してください。

入力

入力は複数のテストケースから構成されます。入力の最初の行には、テストケースの数を示す単一の整数 $T$ ($1 \le T$) が含まれます。

各テストケースの最初の行には、単一の整数 $n$ ($1 \le n \le 200$) が含まれます。続く $n$ 行の各行には $n$ 個の整数が含まれます。$i$ 番目の行には $a_{i,1}, a_{i,2}, \dots, a_{i,n}$ ($1 \le a_{i,j} \le n$) が含まれており、これは Little Cyan Fish が主張する、頂点 $i$ から探索を開始したときに生成される DFS 順序です。$a_{i,1} = i$ であること、および各行 $a_i$ が $1, 2, \dots, n$ の順列であることが保証されます。

すべてのテストケースにおける $n^2$ の総和は $4 \times 10^4$ を超えないことが保証されます。

出力

各テストケースについて、適切なグラフが存在しない場合は "No" とだけ出力してください。

そうでない場合は、最初の行に "Yes" と出力してください。次の行に、グラフの辺の数を示す単一の整数 $m$ ($n - 1 \le m \le \frac{n(n-1)}{2}$) を出力してください。

続く $m$ 行の各行には、2つの整数 $u$ と $v$ ($1 \le u, v \le n, u \neq v$) を出力してください。これは頂点 $u$ と $v$ の間の無向辺を表します。結果として得られるグラフは単純かつ連結であり、各頂点 $i$ からの DFS 順序が $a_i$ と一致しなければなりません。

条件を満たすグラフが複数存在する場合は、そのうちのいずれかを出力してください。

入出力例

入力 1

2
3
1 2 3
2 1 3
3 2 1
3
1 2 3
2 3 1
3 1 2

出力 1

Yes
2
1 2
2 3
No

注記

サンプル 1 の説明:最初のテストケースでは、パス $1-2-3$ が有効なグラフです。頂点 1, 2, 3 からの DFS 順序はそれぞれ $[1, 2, 3], [2, 1, 3], [3, 2, 1]$ となります。2 番目のテストケースでは、適切なグラフは存在しません。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.