QOJ.ac

QOJ

Limite de temps : 5.0 s Limite de mémoire : 256 MB Points totaux : 100 Hackable ✓

#9080. 物品交换

Statistiques

有 $n$ 个人排成一排,从左到右编号为 $1$ 到 $n$。对于每个人 $i$,他手中持有一个物品,编号也为 $i$。

每个人都有一个“偏好列表”,即 $1, \dots, n$ 的一个排列。偏好列表描述了个人对这些物品的偏好程度,即如果 $i$ 在某人的列表中比 $j$ 出现得更靠前,我们就可以说他比物品 $j$ 更偏好物品 $i$。

由于人们可以看到邻居手中持有的物品,如果两个相邻的人都更偏好对方手中的物品,他们就可以交换手中的物品。

现在给定所有人的偏好列表以及两个整数 $i$ 和 $j$,你需要回答是否存在一种交换方案,使得在某个时刻,人 $j$ 持有物品 $i$。

输入格式

输入文件的第一行包含一个整数 $T$ ($1 \le T \le 85$),表示测试用例的数量。 每个测试用例包含多行。 第一行包含一个整数 $n$ ($1 \le n \le 200$),含义如上所述。 接下来 $n$ 行,每行包含 $n$ 个 $1$ 到 $n$ 之间的数字。其中第 $i$ 行描述了人 $i$ ($1 \le i \le n$) 的“偏好列表”。 第 $(n + 2)$ 行包含两个整数 $i$ 和 $j$ ($1 \le i \le n, 1 \le j \le n$),含义如上所述。 其中 $n \ge 100$ 的测试用例不超过 $14$ 个。

输出格式

你需要输出恰好 $T$ 行。 对于每个测试用例,首先打印 Case d:($d$ 表示用例编号),然后在同一行打印答案 YesNo

样例

输入 1

3
3
3 1 2
1 3 2
2 3 1
1 2
3
3 1 2
1 3 2
2 3 1
1 3
4
3 1 2 4
4 1 3 2
1 4 2 3
2 4 1 3
1 3

输出 1

Case 1: Yes
Case 2: No
Case 3: Yes

说明

样例 3 的一种可能方案为: $(1, 2, 3, 4) \to (1, 3, 2, 4) \to (3, 1, 2, 4) \to (3, 1, 4, 2) \to (3, 4, 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.