QOJ.ac

QOJ

Límite de tiempo: 1 s Límite de memoria: 1024 MB Puntuación total: 100

#3612. 家族成员

Estadísticas

家庭遗传研究所的科学家们正在追踪一种遗传病在家族树中的传播情况。他们首先列出了患病的家庭成员,以及将疾病传给每个孩子的父母(我们假设每个孩子只从一位父母那里获得疾病)。然而,科学家们对不同亲属关系的名称感到困惑。对于父母、祖父母和兄弟姐妹,他们还能应付,但像“third cousin, twice removed”(三代堂表亲,隔两代)这样的关系让他们感到头疼。经过多次讨论,他们提出了一些明确的定义。

假设我们有两个方便起见分别命名为 $A$ 和 $B$ 的人,他们最近的共同祖先命名为 $C$。如果从 $C$ 开始有 $m$ 代直系后代到达 $A$,我们就说 $A$ 距离 $C$ 有 $m$ 代。因此,如果 $A$ 是 $C$ 的女儿,她距离 $C$ 有 1 代;如果她是 $C$ 的孙女,她距离 $C$ 有 2 代,依此类推。任何人距离自己都是 0 代。

现在设 $A$ 距离 $C$ 有 $m$ 代,$B$ 距离 $C$ 有 $n$ 代,其中 $m \le n$。我们可以根据以下规则确定 $A$ 和 $B$ 之间的关系:

  1. 如果 $m = 0$,那么当 $n = 1$ 时,$B$ 是 $A$ 的孩子;当 $n > 1$ 时,$B$ 是 $A$ 的 $n-2$ 代孙辈(great...grandchild)。
  2. 如果 $0 < m = n$,那么当 $n = 1$ 时,$A$ 和 $B$ 是兄弟姐妹;当 $n > 1$ 时,$A$ 和 $B$ 是 $n-1$ 代堂表亲($(n-1)$-th cousins)。
  3. 如果 $0 < m < n$,那么 $A$ 和 $B$ 是 $m-1$ 代堂表亲,且隔了 $n-m$ 代($(m-1)$-th cousins, $(n-m)$ times removed)。

注意,如果 $m = 1$ 且 $n = 2$,我们得到有趣的名称“0th cousins, 1 time removed”,这通常被描述为“姑姨叔伯/侄甥”。

下图展示了两个新成员 $D$ 和 $E$ 的一些关系示例。

距离共同祖先的代数
$D$ $E$ 关系
0 1 $E$ 是 $D$ 的孩子
4 0 $D$ 是 $E$ 的玄孙
3 3 $D$ 和 $E$ 是 2nd cousins
9 8 $D$ 和 $E$ 是 7th cousins, 1 time removed
1 4 $D$ 和 $E$ 是 0th cousins, 3 times removed

图 1:部分关系示例

科学家们给了你一份家族树的描述以及树中成对的人员,并要求你确定每一对成员之间的关系。

输入格式

输入的第一行包含两个正整数 $t$ ($t \le 100$) 和 $p$ ($p \le 1000$),分别表示家族树描述的数量和查询对的数量。接下来是 $t$ 行,每行包含一个家族树描述。每个家族树描述的形式为 $s_0\ d\ s_1\ s_2\ \dots\ s_d$,表示人 $s_0$ 有 $d$ 个孩子,名字分别为 $s_1$ 到 $s_d$。所有名字都是唯一的,且仅包含英文字母。家族树描述可以以任何顺序给出(即整个家族树的根节点不一定出现在第一个描述中)。没有名字会在家族树描述的 $s_0$ 位置出现超过一次。所有家族树描述将组合成一棵完整的树,该树至少有 2 个节点,最多有 100 个节点。

随后是 $p$ 行,每行形式为 $s_i\ s_j$,其中 $s_i \neq s_j$,且保证两个名字都在树中。

输出格式

为每一对人员输出关系,每行一个,使用图 1 所示的格式。对于每一对,总是先输出 $s_i$ 的名字,除非 $s_j$ 是 $s_i$ 的直系后代(如图 1 中的第一个例子)。对于第 $n$ 个序数,输出 nth,但 $n = 1, 2, 3, 21, 22, 23, 31, 32, 33, \dots$ 等情况除外,此时应输出 1st, 2nd, 3rd, 21st, 22nd, 23rd, 31st, 32nd, 33rd 等。此外,请确保对于所有隔代数(times removed),除了 1 次外,都使用 times,对于 1 次,应使用单词 time

样例

样例输入 1

4 5
Horatio 1 Irene
Chris 2 Gina Horatio
Alice 3 Dan Emily Frank
Bob 2 Alice Chris
Irene Bob
Dan Frank
Chris Emily
Alice Chris
Dan Irene

样例输出 1

Irene is the great grandchild of Bob
Dan and Frank are siblings
Chris and Emily are 0th cousins, 1 time removed
Alice and Chris are siblings
Dan and Irene are 1st cousins, 1 time removed

样例输入 2

4 6
A 4 B C D E
H 3 I J K
C 2 F G
D 1 H
G C
H A
F G
F H
F K
B K

样例输出 2

G is the child of C
H is the grandchild of A
F and G are siblings
F and H are 1st cousins
F and K are 1st cousins, 1 time removed
B and K are 0th cousins, 2 times removed

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.