• Codeforces Round #396 (Div. 2) D题Mahmoud and a Dictionary(并查集)解题报告


    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.

    Input

    The first line of input contains three integers nm and q (2 ≤ n ≤ 1051 ≤ 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 a1, a2, ..., an 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 xi and yi which has appeared in the dictionary words. If t = 1, that means xi has a synonymy relation with yi, otherwise xi has an antonymy relation with yi.

    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.

    Output

    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.

    Example

    Input
    3 3 4
    hate love like
    1 love like
    2 love hate
    1 hate like
    love like
    love hate
    like hate
    hate like
    Output
    YES
    YES
    NO
    1
    2
    2
    2
    Input
    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
    Output
    YES
    YES
    YES
    YES
    NO
    YES
    3
    3
    1
    1
    2

    解题思路:

      使用map建立单词与序号的对应,方便处理。接下来进行并查集的操作,i+n表示n的反义词,在一个集合中表示同时成立。如果x和y有同义词的关系,检验x和y的反义词、y和x的反义词是否在一个集合中,如果在一个集合中,产生矛盾,输出NO,若不然,成立,就将x和y、x的反义词和y的反义词分别归并入两个集合。反之同理,进行操作即可。

    (并查集处理过程的压缩十分重要,不压缩就很有可能超时)

    参考代码:

     1 #include<bits/stdc++.h>
     2 #include <iostream>
     3 using namespace std;
     4 typedef long long ll;
     5 typedef unsigned long long ull;
     6 int n,m,q;
     7 const int MAX=1e5+7;
     8 int a[MAX*2];
     9 char b[30],c[30];
    10 map<string,int> re;
    11 int gx;
    12 int origin(int x)
    13 {
    14     if(a[x]==x)
    15         return x;
    16     else return a[x]=origin(a[x]);
    17 }
    18 int main()
    19 {
    20     scanf("%d %d %d",&n,&m,&q);
    21     int i;
    22     int x,y;
    23     for(i=0;i<n;i++)
    24     {
    25         scanf("%s",b);
    26         re[b]=i;
    27     }
    28     for(i=0;i<n;i++)
    29     {
    30         a[i]=i;
    31         a[i+n]=i+n;
    32     }
    33     for(i=0;i<m;i++)
    34     {
    35         scanf("%d %s %s",&gx,b,c);
    36         x=re[b];y=re[c];
    37         if(gx==1)
    38         {
    39             if(origin(x)==origin(y+n)||origin(x+n)==origin(y))
    40                 printf("NO
    ");
    41             else
    42             {
    43                 printf("YES
    ");
    44                 a[origin(x)]=a[origin(y)];
    45                 a[origin(x+n)]=a[origin(y+n)];
    46             }
    47         }
    48         else
    49         {
    50             if(origin(x)==origin(y)||origin(x+n)==origin(y+n))
    51                 printf("NO
    ");
    52             else
    53             {
    54                 printf("YES
    ");
    55                 a[origin(x)]=a[origin(y+n)];
    56                 a[origin(x+n)]=a[origin(y)];
    57             }
    58         }
    59     }
    60     for(i=0;i<q;i++)
    61     {
    62         scanf("%s %s",b,c);
    63         x=re[b];y=re[c];
    64         if(origin(x)==origin(y)||origin(x+n)==origin(y+n))
    65             printf("1
    ");
    66         else if(origin(x)==origin(y+n)||origin(x+n)==origin(y))
    67             printf("2
    ");
    68         else printf("3
    ");
    69     }
    70     return 0;
    71 }
  • 相关阅读:
    数据库连接,报错--mysql版本不匹配
    SpringMVC项目如何添加事物呢
    将存放数字的list,顺序排列,然后,判断,数字是否是连续的
    list从小到大,排序----这么简单
    SpringMVC控制层,setViewName--不能跳转到指定视图
    SpringMVC中jsp和controller互传参的问题
    jsp到controller乱码
    PDF 补丁丁 0.4.1 版:新增嵌入中文字库、替换文档字库的功能
    PDF 补丁丁 0.4.1 版将增加嵌入中文字库的功能
    Django视图层
  • 原文地址:https://www.cnblogs.com/quintessence/p/6384642.html
Copyright © 2020-2023  润新知