Mahmoud and a Dictionary
4 seconds
256 megabytes
standard input
standard output
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 meanslike, 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.
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 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.
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.
- 3 3 4
hate love like
1 love like
2 love hate
1 hate like
love like
love hate
like hate
hate like
- YES
YES
NO
1
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
- YES
YES
YES
YES
NO
YES
3
3
1
1
2
分析:带权并查集,修改类似于向量似的修改;
比如合并a和b,x=find(a),y=find(b),
p[x]=y,根连起来,然后col[x]=col[a]^col[b]^(t-1),
即x->a->b->y==x->y,表示x与根y的关系;
代码:
- #include <iostream>
- #include <cstdio>
- #include <cstdlib>
- #include <cmath>
- #include <algorithm>
- #include <climits>
- #include <cstring>
- #include <string>
- #include <set>
- #include <bitset>
- #include <map>
- #include <queue>
- #include <stack>
- #include <vector>
- #define rep(i,m,n) for(i=m;i<=n;i++)
- #define mod 1000000007
- #define inf 0x3f3f3f3f
- #define vi vector<int>
- #define pb push_back
- #define mp make_pair
- #define fi first
- #define se second
- #define ll long long
- #define pi acos(-1.0)
- #define pii pair<int,int>
- #define sys system("pause")
- const int maxn=1e5+;
- const int N=5e4+;
- const int M=N**;
- using namespace std;
- inline ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
- inline ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
- inline void umax(ll &p,ll q){if(p<q)p=q;}
- inline void umin(ll &p,ll q){if(p>q)p=q;}
- inline ll read()
- {
- ll x=;int f=;char ch=getchar();
- while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
- while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
- return x*f;
- }
- int n,m,k,t,p[maxn],col[maxn],q;
- map<string,int>id;
- char a[],b[];
- int find(int x)
- {
- if(x==p[x])return x;
- else
- {
- int y=p[x];
- p[x]=find(p[x]);
- col[x]^=col[y];
- return p[x];
- }
- }
- int main()
- {
- int i,j;
- scanf("%d%d%d",&n,&m,&q);
- rep(i,,n)
- {
- scanf("%s",a);
- id[a]=i;
- p[i]=i;
- }
- rep(i,,m)
- {
- scanf("%d%s%s",&t,a,b);
- int x=find(id[a]),y=find(id[b]);
- if(x==y)
- {
- if((col[id[a]]^col[id[b]])+!=t)puts("NO");
- else puts("YES");
- }
- else
- {
- puts("YES");
- p[x]=y;
- col[x]=col[id[a]]^col[id[b]]^(t-);
- }
- }
- rep(i,,q)
- {
- scanf("%s%s",a,b);
- int x=find(id[a]),y=find(id[b]);
- if(x!=y)puts("");
- else printf("%d\n",(col[id[a]]^col[id[b]])+);
- }
- return ;
- }
Mahmoud and a Dictionary的更多相关文章
- Codeforces 766D. Mahmoud and a Dictionary 并查集 二元敌对关系 点拆分
D. Mahmoud and a Dictionary time limit per test:4 seconds memory limit per test:256 megabytes input: ...
- Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary 并查集
D. Mahmoud and a Dictionary 题目连接: http://codeforces.com/contest/766/problem/D Description Mahmoud wa ...
- Codeforces 766D Mahmoud and a Dictionary 2017-02-21 14:03 107人阅读 评论(0) 收藏
D. Mahmoud and a Dictionary time limit per test 4 seconds memory limit per test 256 megabytes input ...
- Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary
地址:http://codeforces.com/contest/766/problem/D 题目: D. Mahmoud and a Dictionary time limit per test 4 ...
- Codefroces 766D Mahmoud and a Dictionary
D. Mahmoud and a Dictionary time limit per test 4 seconds memory limit per test 256 megabytes input ...
- cf776D Mahmoud and a Dictionary
Mahmoud wants to write a new dictionary that contains n words and relations between them. There are ...
- 【codeforces 766D】Mahmoud and a Dictionary
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- codeforces#766 D. Mahmoud and a Dictionary (并查集)
题意:给出n个单词,m条关系,q个询问,每个对应关系有,a和b是同义词,a和b是反义词,如果对应关系无法成立就输出no,并且忽视这个关系,如果可以成立则加入这个约束,并且输出yes.每次询问两个单词的 ...
- CodeForces 766D Mahmoud and a Dictionary
并查集. 将每一个物品拆成两个,两个意义相反,然后并查集即可. #pragma comment(linker, "/STACK:1024000000,1024000000") #i ...
随机推荐
- caioj1495: [视频]基于连通性状态压缩的动态规划问题:Formula 2
本来想写一天插头的,但是这题太难受(绝望)500+的代码量..我选择下午放松一下. 先ORZ一下苏大佬(yz的cdq啊%%%%%)居然把cdq论文里面的题抠出来出数据放在c站(呵呵真是个悲伤的故事不过 ...
- CockroachDB——类似spanner的开源版,底层使用rocksdb存储,mvcc,支持事务,raft一致性,licence是CockroachDB Community License Agreement
摘自:https://github.com/cockroachdb/cockroach/blob/master/docs/design.md CockroachDB is a distributed ...
- KMP字符匹配算法
上个假期就学了KMP,但是基本不用,所以忘干净了...这个的核心思想就是next数组,next数组学名叫最长相同前缀后缀.还不错的算法,KMP 匹配的过程中比原来的暴力匹配多了一个跳来跳去的next. ...
- 线性回归(最小二乘法、批量梯度下降法、随机梯度下降法、局部加权线性回归) C++
We turn next to the task of finding a weight vector w which minimizes the chosen function E(w). Beca ...
- Gold Balanced Lineup(hash)
http://poj.org/problem?id=3274 ***** #include <stdio.h> #include <iostream> #include < ...
- javascript 原型(prototype 、__proto__、函数、对象)
一.类型 1.JavaScript中分为值类型(string/boolean/null/number/undefind).引用类型(数组.对象.函数): 2.数组.函数.对象都是对象: 对象是由函数创 ...
- python 46 盒模型 与盒模型布局
一:盒模型 1. 盒模型的概念 广义盒模型:文档中所有功能性及内容性标签,及文档中显示性标签 侠义盒模型:文档中以块级形式存在的标签(块级标签拥有盒模型100%特性且最常用) 盒模型组成:margi ...
- RecastNavigation(3D场景建模、网格导航)
一.RecastNavigation详解 RecastNavigation定义: RecastNavigation是一个导航寻路工具集,使用邻接的凸多边形集合描述一个3D场景,A*寻路算法使3D场景的 ...
- HDU 1054 Hungary
Strategic Game Problem Description Bob enjoys playing computer games, especially strategic games, bu ...
- wap 5.23 网测几道题目
1. n个犯人,m个省份, 如果相邻的2个犯人来自同一省份,则是不安全的,求不安全的个数. 正难则反,用全部的个数减去非法的个数,就是最后的答案. m^n - m * (m - 1) ^ (n - 1 ...