CF766D.Mahmoud and a Dictionary

普及+/提高

通过率:0%

时间限制:4.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Mahmoud wants to write a new dictionary that contains n words and relations between them. There are two types of relations: synonymy (i. e. the two words mean the same) and antonymy (i. e. the two words mean the opposite). From time to time he discovers a new relation between two words.

He know that if two words have a relation between them, then each of them has relations with the words that has relations with the other. For example, if like means love and love is the opposite of hate, then like is also the opposite of hate. One more example: if love is the opposite of hate and hate is the opposite of like, then love means like, and so on.

Sometimes Mahmoud discovers a wrong relation. A wrong relation is a relation that makes two words equal and opposite at the same time. For example if he knows that love means like and like is the opposite of hate, and then he figures out that hate means like, the last relation is absolutely wrong because it makes hate and like opposite and have the same meaning at the same time.

After Mahmoud figured out many relations, he was worried that some of them were wrong so that they will make other relations also wrong, so he decided to tell every relation he figured out to his coder friend Ehab and for every relation he wanted to know is it correct or wrong, basing on the previously discovered relations. If it is wrong he ignores it, and doesn't check with following relations.

After adding all relations, Mahmoud asked Ehab about relations between some words based on the information he had given to him. Ehab is busy making a Codeforces round so he asked you for help.

马哈茂德想要编写一本新词典,其中包含 nn 个单词以及它们之间的关系。关系分为两类:同义关系(即两个单词含义相同)和反义关系(即两个单词含义相反)。他不时会发现两个单词之间存在新的关系。

他了解到:若两个单词之间存在某种关系,则每个单词与另一个单词的所有相关单词之间也必然存在相应的关系。例如,若 like 同义于 love,而 love 反义于 hate,则 like 也反义于 hate。再举一例:若 love 反义于 hate,且 hate 反义于 like,则 love 同义于 like,依此类推。

有时,马哈茂德会发现一条错误的关系。所谓错误的关系,是指会导致某两个单词同时具有同义与反义关系的关系。例如,若他已知 love 同义于 like,且 like 反义于 hate,而后他又得出 hate 同义于 like,那么最后这条关系显然是错误的,因为它使得 hate 与 like 既互为反义又互为同义。

在发现大量关系后,马哈茂德担心其中某些关系是错误的,从而导致其他关系也出错。因此,他决定将自己发现的每一条关系依次告诉他的程序员朋友埃哈卜,并针对每条关系询问:基于此前已知的所有关系,该关系是否正确?若该关系错误,则忽略它,且后续关系也不再以此错误关系为依据进行验证。

在添加完所有关系后,马哈茂德向埃哈卜询问若干单词对之间的关系(基于他此前提供给埃哈卜的所有信息)。埃哈卜正忙于筹备一场 Codeforces 比赛,于是他请你帮忙解决这个问题。

输入格式

The first line of input contains three integers n, m and q (2 ≤ n ≤ 105, 1 ≤ m, q ≤ 105) where n is the number of words in the dictionary, m is the number of relations Mahmoud figured out and q is the number of questions Mahmoud asked after telling all relations.

The second line contains n distinct words _a_1, _a_2, ..., a__n consisting of small English letters with length not exceeding 20, which are the words in the dictionary.

Then m lines follow, each of them contains an integer t (1 ≤ t ≤ 2) followed by two different words x__i and y__i which has appeared in the dictionary words. If t = 1, that means x__i has a synonymy relation with y__i, otherwise x__i has an antonymy relation with y__i.

Then q lines follow, each of them contains two different words which has appeared in the dictionary. That are the pairs of words Mahmoud wants to know the relation between basing on the relations he had discovered.

All words in input contain only lowercase English letters and their lengths don't exceed 20 characters. In all relations and in all questions the two words are different.

输入的第一行包含三个整数 nn、mm 和 qq(2≤n≤1052 \leq n \leq 10^5,1≤m,q≤1051 \leq m, q \leq 10^5),其中 nn 表示字典中单词的个数,mm 表示 Mahmoud 推断出的关系数量,qq 表示 Mahmoud 在告知所有关系后所提出的询问数量。

第二行包含 nn 个互不相同的单词 a1, a2, …, ana_1,\ a_2,\ \dots,\ a_n,均由小写英文字母组成,且每个单词长度不超过 20,这些即为字典中的单词。

接下来是 mm 行,每行包含一个整数 tt(1≤t≤21 \leq t \leq 2)以及两个在字典中出现过的不同单词 xix_i 和 yiy_i。若 t=1t = 1,表示 xix_i 与 yiy_i 具有同义关系;否则(即 t=2t = 2),表示 xix_i 与 yiy_i 具有反义关系。

随后是 qq 行,每行包含两个在字典中出现过的不同单词,即 Mahmoud 希望基于已发现的关系判断其相互关系的单词对。

输入中所有单词仅由小写英文字母组成,且长度均不超过 20 个字符。在所有关系及所有询问中,两个单词均互不相同。

输出格式

First, print m lines, one per each relation. If some relation is wrong (makes two words opposite and have the same meaning at the same time) you should print "NO" (without quotes) and ignore it, otherwise print "YES" (without quotes).

After that print q lines, one per each question. If the two words have the same meaning, output 1. If they are opposites, output 2. If there is no relation between them, output 3.

See the samples for better understanding.

首先,输出 m 行,每行对应一个关系。如果某个关系是错误的(即导致两个单词既互为反义词又具有相同含义),则输出 "NO"(不带引号)并忽略该关系;否则输出 "YES"(不带引号)。

之后,输出 q 行,每行对应一个问题。如果两个单词含义相同,则输出 1;如果它们互为反义词,则输出 2;如果它们之间没有关系,则输出 3。

请参阅样例以更好地理解。

输入输出样例

  • 输入#1

    3 3 4
    hate love like
    1 love like
    2 love hate
    1 hate like
    love like
    love hate
    like hate
    hate like

    输出#1

    YES
    YES
    NO
    1
    2
    2
    2
  • 输入#2

    8 6 5
    hi welcome hello ihateyou goaway dog cat rat
    1 hi welcome
    1 ihateyou goaway
    2 hello ihateyou
    2 hi goaway
    2 hi hello
    1 hi hello
    dog cat
    dog hi
    hi hello
    ihateyou goaway
    welcome ihateyou

    输出#2

    YES
    YES
    YES
    YES
    NO
    YES
    3
    3
    1
    1
    2

输入解题思路,AI测评打分。不知道怎么写?

首页