codeforce 766D Mahmoud and a Dictionary 带权并查集
来源:互联网 发布:php开发实战视频教程 编辑:程序博客网 时间:2024/06/05 16:14
传送门
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 likeand 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.
The first line of input contains three integers n, m and q (2 ≤ n ≤ 105, 1 ≤ m, q ≤ 105) where nis 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 20characters. 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 4hate love like1 love like2 love hate1 hate likelove likelove hatelike hatehate like
YESYESNO1222
8 6 5hi welcome hello ihateyou goaway dog cat rat1 hi welcome1 ihateyou goaway2 hello ihateyou2 hi goaway2 hi hello1 hi hellodog catdog hihi helloihateyou goawaywelcome ihateyou
YESYESYESYESNOYES33112
学习带权并查集get√
//china no.1#pragma comment(linker, "/STACK:1024000000,1024000000")#include <vector>#include <iostream>#include <string>#include <map>#include <stack>#include <cstring>#include <queue>#include <list>#include <stdio.h>#include <set>#include <algorithm>#include <cstdlib>#include <cmath>#include <iomanip>#include <cctype>#include <sstream>#include <functional>#include <stdlib.h>#include <time.h>#include <bitset>using namespace std;#define pi acos(-1)#define endl '\n'#define srand() srand(time(0));#define me(x,y) memset(x,y,sizeof(x));#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)#define close() ios::sync_with_stdio(0); cin.tie(0);#define FOR(x,n,i) for(int i=x;i<=n;i++)#define FOr(x,n,i) for(int i=x;i<n;i++)#define W while#define sgn(x) ((x) < 0 ? -1 : (x) > 0)#define bug printf("***********\n");typedef long long LL;const int INF=0x3f3f3f3f;const LL LINF=0x3f3f3f3f3f3f3f3fLL;const int dx[]={-1,0,1,0,1,-1,-1,1};const int dy[]={0,1,0,-1,-1,1,-1,1};const int maxn=1e3+10;const int maxx=3e5+100;const double EPS=1e-7;const int mod=10000007;#define mod(x) ((x)%MOD);template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}inline LL Scan(){ int f=1;char C=getchar();LL x=0; while (C<'0'||C>'9'){if (C=='-')f=-f;C=getchar();} while (C>='0'&&C<='9'){x=x*10+C-'0';C=getchar();} x*=f;return x;}//freopen( "in.txt" , "r" , stdin );//freopen( "data.out" , "w" , stdout );//cerr << "run time is " << clock() << endl;map<string,int>mp;int fa[maxx],height[maxx];void init(int n){ for(int i=1;i<=3*n;i++) { fa[i]=i; height[i]=0; }}int fi(int x){ //路径压缩,把所有元素指向根 return fa[x]==x?x:fi(fa[x]);}void unite(int x, int y){ x = fi(x); y = fi(y); if (x == y) return ; if (height[x] < height[y]) { fa[x] = y; } else { fa[y] = x; if (height[x] == height[y]) height[x]++; }}int main(){ int n,m,q,z; cin>>n>>m>>q; string s,s1,s2; for(int i=1;i<=n;i++) { cin>>s; mp[s]=i; } init(n); FOR(1,m,i) { cin>>z>>s1>>s2; int x=mp[s1],y=mp[s2]; if(z==1) { if(fi(x+n)==fi(y)||fi(x)==fi(y+n)) printf("NO\n"); else { printf("YES\n"); unite(x,y); unite(x+n,y+n); } } else { if(fi(x)==fi(y)) printf("NO\n"); else { printf("YES\n"); unite(x,y+n); unite(x+n,y); } } } FOR(1,q,i) { cin>>s1>>s2; int x=mp[s1],y=mp[s2]; if(fi(x)==fi(y)) puts("1"); else if(fi(x+n)==fi(y)||fi(x)==fi(y+n)) puts("2"); else puts("3"); }}
- codeforce 766D Mahmoud and a Dictionary 带权并查集
- D. Mahmoud and a Dictionary----带权并查集
- cf 766 d Mahmoud and a Dictionary(带权并查集)
- codeforces 766D Mahmoud and a Dictionary(带权并查集+map查询)
- 766D Mahmoud and a Dictionary[带权并查集][dfs]
- codeforces-766D-Mahmoud and a Dictionary(带权并查集)
- 【带权并查集】Mahmoud and a Dictionary
- Codeforces Round #396 (Div. 2)D. Mahmoud and a Dictionary(带权并查集)
- Codeforces Round #396 (Div. 2)D. Mahmoud and a Dictionary【带权并查集】
- Codeforces Round #396 (Div. 2)D. Mahmoud and a Dictionary(带权并查集)
- D. Mahmoud and a Dictionary(种类并查集)
- 并查集(Mahmoud and a Dictionary,cf 766D)
- CodeForces - 766D Mahmoud and a Dictionary (并查集)
- Codeforces 766D Mahmoud and a Dictionary(并查集+map)
- Codeforces Round #396 (Div. 2)-D. Mahmoud and a Dictionary(关系并查集)
- Codeforces Round #396 (Div. 2)-D. Mahmoud and a Dictionary(并查集)
- Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary(并查集)
- Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary (并查集)
- 认识linux 线程
- C#调用WPS操作Excel文件遇到的问题
- 抽象工厂模式-与-工厂方法模式区别
- 在ubuntu14.04下openni+opencv+kinectV1的学习五:边缘检测
- spring学习----AOP
- codeforce 766D Mahmoud and a Dictionary 带权并查集
- hdu6090 数学思维+完全图+星形图的了解
- bzoj 1901: Zju2112 Dynamic Rankings(离线树状数组+主席树)
- Android 自定义圆形头像
- Js_面向对象和多媒体
- Adapter刷新时,列表中ImageView闪烁
- Interface是否继承了Object类
- 那些年寮里的崽儿们 CSU
- 设计模式学习笔记十三:外观模式(Facade Pattern)