QOJ.ac

QOJ

时间限制: 10 s - 20 s 内存限制: 1024 MB 总分: 32

#5956. 悖论排序

统计

Vlad 喜欢糖果。你有一袋不同的糖果,你打算让 Vlad 保留其中一颗。你选择一个糖果的顺序,然后一次给 Vlad 一颗。对于 Vlad 收到的每一颗糖果(第一颗除外),他会将刚收到的糖果与他手中已有的糖果进行比较,保留他更喜欢的那一颗,并扔掉另一颗。

你可能认为无论你选择什么顺序,Vlad 最终总会得到他最喜欢的糖果。但事实并非如此!他并不一定有一个“最喜欢”的糖果。我们知道他对于任意一对糖果的偏好,但他的选择并不一定对应于一个简单的排名。例如,当在橙子和柠檬之间选择时,他可能选橙子;在橙子和香蕉之间选择时,他选香蕉;而在柠檬和香蕉之间选择时,他却选柠檬!

你希望 Vlad 最终保留特定的某一颗糖果。给定 Vlad 对每一对糖果的偏好,确定是否存在一种顺序,使得 Vlad 最终保留的是你指定的糖果。如果存在,请找出字典序最小的这种顺序。

输入格式

输入的第一行包含测试用例的数量 $T$。接下来是 $T$ 个测试用例。每个测试用例的第一行包含两个整数 $N$ 和 $A$,中间用空格分隔。$N$ 是糖果的数量,$A$ 是我们希望 Vlad 最终保留的糖果编号。糖果编号从 $0$ 到 $N-1$。接下来的 $N$ 行,每行包含 $N$ 个字符。第 $i$ 行的第 $j$ 个字符,如果 Vlad 喜欢糖果 $i$ 胜过糖果 $j$,则为 'Y';如果 Vlad 喜欢糖果 $j$ 胜过糖果 $i$,则为 'N';如果 $i = j$,则为 '-'。注意,如果 $i \neq j$,第 $i$ 行的第 $j$ 个字符必须与第 $j$ 行的第 $i$ 个字符不同。

输出格式

对于每个测试用例,输出 "Case #x: ",其中 $x$ 是用例编号,随后输出 "IMPOSSIBLE" 或者以空格分隔的、能使 Vlad 最终保留 $A$ 的字典序最小的糖果顺序。

样例

输入格式 1

3
2 0
-Y
N-
2 0
-N
Y-
4 3
-YNN
N-YY
YN-Y
YNN-

输出格式 1

Case #1: 0 1
Case #2: IMPOSSIBLE
Case #3: 1 2 0 3

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.