Codeforces 766D Mahmoud and a Dictionary(并查集+map)
2017-10-02 21:24
281 查看
思路:
对于同义词和反义词分别考虑最一般的形式。即,对于集合x,y和其相应的反义词的集合x+n,y+n,讨论其关系。
#include <string.h> #include <iostream> #include <cstdio> #include <queue> #include<algorithm> #include <map> using namespace std; typedef long long int lli; #define inf 0x3f3f3f3f const int maxn = 202000; int nu[maxn]; int find(int x){ return nu[x] = nu[x] == x ? x : find(nu[x]); } bool merge(int x,int y){ int xx = find(x),yy = find(y); if(xx != yy) nu[yy] = xx; else return 1; return 0; } char s[200]; map<string,int> ma; int main(){ for(int i = 1;i <= 200100;i++){ nu[i] = i; } int n,m,q; scanf("%d%d%d",&n,&m,&q); for(int i = 1;i <= n;i++){ scanf("%s",s); ma[s] = i; } int flag,x,y; for(int i = 1;i <= m;i++){ scanf("%d%s",&flag,s); x = ma[s]; scanf("%s",s); y = ma[s]; int f1,f2,f3,f4; f1 = find(x);f2 = find(x+n); f3 = find(y);f4 = find(y+n); if(flag == 1){ if(f1 == f4 || f2 == f3){ puts("NO"); } else{ merge(f1,f3);merge(f4,f2); puts("YES"); } } else{ if(f1 == f3 || f4 == f2){ puts("NO"); } else{ merge(f2,f3);merge(f4,f1); puts("YES"); } } } for(int i = 1;i <= q;i++){ int t1,t2; scanf("%s",s);x = ma[s]; t1 = find(ma[s]); scanf("%s",s);y = ma[s]; t2 = find(ma[s]); if(t1 == t2){ puts("1"); } else if(t1 == find(y+n)){ puts("2"); } else puts("3"); } }
相关文章推荐
- Codeforces 766D Mahmoud and a Dictionary
- CodeForces - 766D Mahmoud and a Dictionary
- CodeForces-766D Mahmoud and a Dictionary 并查集 维护同类不同类元素集合
- codeforce 766D Mahmoud and a Dictionary 带权并查集
- codeforces-766D-Mahmoud and a Dictionary(带权并查集)
- codeforce 766D Mahmoud and a Dictionary 带权并查集
- Codefroces 766D Mahmoud and a Dictionary
- codeforce 766D Mahmoud and a Dictionary 带权并查集
- Mahmoud and a Dictionary CodeForces - 766D 种类并查集
- Mahmoud and a Dictionary CodeForces - 766D
- Mahmoud and a Dictionary CodeForces - 766D
- codeforce 766D Mahmoud and a Dictionary 带权并查集
- codeforce 766D Mahmoud and a Dictionary 带权并查集
- codeforce 766D Mahmoud and a Dictionary 带权并查集
- codeforce 766D Mahmoud and a Dictionary 带权并查集
- codeforces 766D. Mahmoud and a Dictionary
- codeforce 766D Mahmoud and a Dictionary 带权并查集
- codeforce 766D Mahmoud and a Dictionary 带权并查集
- Codeforces #396 (Div. 2) D. Mahmoud and a Dictionary (并查集+map
- codeforce 766D Mahmoud and a Dictionary 带权并查集