B. Innokenty and a Football League

Codeforces
IDCF781B
Time2000ms
Memory256MB
Difficulty
2-satbrute forcegraph matchingsgraphsgreedyimplementationshortest pathsstrings
English · Original
Chinese · Translation
Formal · Original
Innokenty is a president of a new football league in Byteland. The first task he should do is to assign short names to all clubs to be shown on TV next to the score. Of course, the short names should be distinct, and Innokenty wants that all short names consist of **three letters**. Each club's full name consist of two words: the team's name and the hometown's name, for example, "_DINAMO BYTECITY_". Innokenty doesn't want to assign strange short names, so he wants to choose such short names for each club that: 1. the short name is the same as three first letters of the team's name, for example, for the mentioned club it is "_DIN_", 2. or, the first two letters of the short name should be the same as the first two letters of the team's name, while the third letter is the same as the first letter in the hometown's name. For the mentioned club it is "_DIB_". Apart from this, there is a rule that if for some club _x_ the second option of short name is chosen, then there should be no club, for which the first option is chosen which is the same as the first option for the club _x_. For example, if the above mentioned club has short name "_DIB_", then no club for which the first option is chosen can have short name equal to "_DIN_". However, it is possible that some club have short name "_DIN_", where "_DI_" are the first two letters of the team's name, and "_N_" is the first letter of hometown's name. Of course, no two teams can have the same short name. Help Innokenty to choose a short name for each of the teams. If this is impossible, report that. If there are multiple answer, any of them will suit Innokenty. If for some team the two options of short name are equal, then Innokenty will formally think that only one of these options is chosen. ## Input The first line contains a single integer _n_ (1 ≤ _n_ ≤ 1000) — the number of clubs in the league. Each of the next _n_ lines contains two words — the team's name and the hometown's name for some club. Both team's name and hometown's name consist of uppercase English letters and have length at least 3 and at most 20. ## Output It it is not possible to choose short names and satisfy all constraints, print a single line "_NO_". Otherwise, in the first line print "_YES_". Then print _n_ lines, in each line print the chosen short name for the corresponding club. Print the clubs in the same order as they appeared in input. If there are multiple answers, print any of them. [samples] ## Note In the first sample Innokenty can choose first option for both clubs. In the second example it is not possible to choose short names, because it is not possible that one club has first option, and the other has second option if the first options are equal for both clubs. In the third example Innokenty can choose the second options for the first two clubs, and the first option for the third club. In the fourth example note that it is possible that the chosen short name for some club _x_ is the same as the first option of another club _y_ if the first options of _x_ and _y_ are different.
Innokenty 是 Byteland 一个新的足球联赛的主席。他需要为所有俱乐部分配简短名称,以便在电视上与比分一起显示。当然,这些简短名称必须互不相同,且 Innokenty 希望所有简短名称均由 *三个字母* 组成。 每个俱乐部的全名由两个单词组成:球队名称和家乡名称,例如 "_DINAMO BYTECITY_"。Innokenty 不希望分配奇怪的简短名称,因此他希望为每个俱乐部选择这样的简短名称: 此外,有一条规则:如果某个俱乐部 #cf_span[x] 选择了第二个简短名称选项,则不存在任何其他俱乐部,其第一个选项与俱乐部 #cf_span[x] 的第一个选项相同。例如,如果上述俱乐部的简短名称为 "_DIB_",则任何选择第一个选项的俱乐部都不能有简短名称 "_DIN_"。但允许某个俱乐部的简短名称为 "_DIN_",其中 "_DI_" 是球队名称的前两个字母,"_N_" 是家乡名称的第一个字母。当然,任何两个球队都不能有相同的简短名称。 帮助 Innokenty 为每个球队选择一个简短名称。如果不可能,请报告出来。如果有多个答案,任选其一即可。如果某个球队的两个选项相同,则 Innokenty 会形式上认为只选择了一个选项。 第一行包含一个整数 #cf_span[n](#cf_span[1 ≤ n ≤ 1000])— 联赛中的俱乐部数量。 接下来的 #cf_span[n] 行,每行包含两个单词 —— 某个俱乐部的球队名称和家乡名称。球队名称和家乡名称均由大写英文字母组成,长度至少为 #cf_span[3],至多为 #cf_span[20]。 如果无法选择满足所有约束的简短名称,请输出一行 "_NO_"。 否则,第一行输出 "_YES_",然后输出 #cf_span[n] 行,每行输出对应俱乐部的选定简短名称。输出顺序应与输入顺序一致。 如果有多个答案,输出任意一个即可。 在第一个样例中,Innokenty 可以为两个俱乐部都选择第一个选项。 在第二个样例中,不可能选择简短名称,因为如果两个俱乐部的第一个选项相同,则不允许一个俱乐部选择第一个选项而另一个选择第二个选项。 在第三个样例中,Innokenty 可以为前两个俱乐部选择第二个选项,为第三个俱乐部选择第一个选项。 在第四个样例中,请注意,如果俱乐部 #cf_span[x] 和俱乐部 #cf_span[y] 的第一个选项不同,则 #cf_span[x] 的选定简短名称可以与 #cf_span[y] 的第一个选项相同。 ## Input 第一行包含一个整数 #cf_span[n](#cf_span[1 ≤ n ≤ 1000])— 联赛中的俱乐部数量。接下来的 #cf_span[n] 行,每行包含两个单词 —— 某个俱乐部的球队名称和家乡名称。球队名称和家乡名称均由大写英文字母组成,长度至少为 #cf_span[3],至多为 #cf_span[20]。 ## Output 如果无法选择满足所有约束的简短名称,请输出一行 "_NO_"。否则,第一行输出 "_YES_",然后输出 #cf_span[n] 行,每行输出对应俱乐部的选定简短名称。输出顺序应与输入顺序一致。如果有多个答案,输出任意一个即可。 [samples] ## Note 在第一个样例中,Innokenty 可以为两个俱乐部都选择第一个选项。在第二个样例中,不可能选择简短名称,因为如果两个俱乐部的第一个选项相同,则不允许一个俱乐部选择第一个选项而另一个选择第二个选项。在第三个样例中,Innokenty 可以为前两个俱乐部选择第二个选项,为第三个俱乐部选择第一个选项。在第四个样例中,请注意,如果俱乐部 #cf_span[x] 和俱乐部 #cf_span[y] 的第一个选项不同,则 #cf_span[x] 的选定简短名称可以与 #cf_span[y] 的第一个选项相同。
**Definitions** Let $ n \in \mathbb{Z} $ be the number of clubs. For each club $ i \in \{1, \dots, n\} $, let: - $ T_i $ be the team’s name (string of uppercase letters, $ 3 \leq |T_i| \leq 20 $), - $ H_i $ be the hometown’s name (string of uppercase letters, $ 3 \leq |H_i| \leq 20 $). Define two candidate short names for club $ i $: - $ A_i = T_i[1:3] $ (first three letters of team name), - $ B_i = T_i[1:2] + H_i[1] $ (first two letters of team name + first letter of hometown name). Let $ S_i \in \{A_i, B_i\} $ be the chosen short name for club $ i $. **Constraints** 1. All chosen short names must be distinct: $$ \forall i, j \in \{1, \dots, n\},\ i \ne j \Rightarrow S_i \ne S_j $$ 2. If $ S_i = B_i $, then for all $ j \ne i $, $ S_j \ne A_i $. (That is, if club $ i $ uses its second option, then no club $ j $ may use its *first* option if that first option equals $ A_i $.) **Objective** Determine if there exists an assignment $ (S_1, \dots, S_n) \in \prod_{i=1}^n \{A_i, B_i\} $ satisfying the above constraints. If yes, output any such assignment; otherwise, output "NO".
Samples
Input #1
2
DINAMO BYTECITY
FOOTBALL MOSCOW
Output #1
YES
DIN
FOO
Input #2
2
DINAMO BYTECITY
DINAMO BITECITY
Output #2
NO
Input #3
3
PLAYFOOTBALL MOSCOW
PLAYVOLLEYBALL SPB
GOGO TECHNOCUP
Output #3
YES
PLM
PLS
GOG
Input #4
3
ABC DEF
ABC EFG
ABD OOO
Output #4
YES
ABD
ABE
ABO
API Response (JSON)
{
  "problem": {
    "name": "B. Innokenty and a Football League",
    "description": {
      "content": "Innokenty is a president of a new football league in Byteland. The first task he should do is to assign short names to all clubs to be shown on TV next to the score. Of course, the short names should ",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF781B"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Innokenty is a president of a new football league in Byteland. The first task he should do is to assign short names to all clubs to be shown on TV next to the score. Of course, the short names should ...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Innokenty 是 Byteland 一个新的足球联赛的主席。他需要为所有俱乐部分配简短名称,以便在电视上与比分一起显示。当然,这些简短名称必须互不相同,且 Innokenty 希望所有简短名称均由 *三个字母* 组成。\n\n每个俱乐部的全名由两个单词组成:球队名称和家乡名称,例如 \"_DINAMO BYTECITY_\"。Innokenty 不希望分配奇怪的简短名称,因此他希望为每个俱乐部选择这...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z} $ be the number of clubs.  \nFor each club $ i \\in \\{1, \\dots, n\\} $, let:  \n- $ T_i $ be the team’s name (string of uppercase letters, $ 3 \\leq |T_i| \\leq 20 $...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments