Codeforces 396 div.2 带权并查集
来源:互联网 发布:766冒险岛数据库进不了 编辑:程序博客网 时间:2024/05/16 03:59
题意:
一共给你N个单词,其中有M个关系是已知的,我们要对Q对单词进行关系查询。
其中M个关系,要么两个单词是同义词,要么两个单词是反义词。
对于查询,如果两个单词是同义词,输出1,如果是反义词输出2,如果不能确定输出3.
分析:
带权并查集。权值的设置需要考虑
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <string>#include <map>using namespace std;typedef long long ll;const int N=1e5+5;int fa[N],ran[N];int _find(int x){ if(fa[x]!=x) { int pre=fa[x]; fa[x]=_find(fa[x]); ran[x]=(ran[x]+ran[pre])%2; //a->fa>ffa //ran[a]是a和fa的关系,ran[pre]是fa和ffa的关系,新的ran[a]是a和ffa的关系,根据关系向量得该表达式 } return fa[x];}int main(){ int n,m,q,r; char sr[25],ssr[25]; scanf("%d%d%d",&n,&m,&q); map<string,int> mp; for(int i=1;i<=n;i++) { scanf("%s",sr); mp[sr]=i; fa[i]=i,ran[i]=0; } for(int i=0;i<m;i++) { scanf("%d%s%s",&r,sr,ssr); int a=mp[sr],b=mp[ssr]; int aa=_find(a),bb=_find(b); if(aa!=bb) { puts("YES"); fa[bb]=aa; ran[bb]=(ran[a]-ran[b]+r+1)%2; } else { n=(ran[a]-ran[b]+2)%2; n==r-1?puts("YES"):puts("NO"); } } while(q--) { scanf("%s%s",sr,ssr); int a=mp[sr],b=mp[ssr]; int aa=_find(a),bb=_find(b); if(aa!=bb)puts("3"); else { n=(ran[a]-ran[b]+2)%2; printf("%d\n",n+1); } }}
0 0
- Codeforces 396 div.2 带权并查集
- 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 #181 (Div. 2) B. Coach 带权并查集
- Codeforces Round #396 D (带权并查集)
- Codeforces Round #254 (Div. 2)(并查集,线段树)
- Codeforces Round #218 (Div. 2)---D. Vessels(并查集)
- Codeforces Round #346 (Div. 2) F bfs+并查集
- Codeforces Round #376 (Div. 2) C 并查集
- Codeforces Round #376 (Div. 2) C dfs+并查集
- Codeforces Round #433 (Div. 2) Planning (贪心+并查集)
- CF Round#396D (Div. 2)(Codeforces 766D) 简单并查集+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 (并查集)
- Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary 并查集
- 从commons-dbcp源码学习设计思路
- 2017.2.11【初中部 GDKOI】模拟赛B组 最小比例(ratio) 题解
- Leetcode 153 - Find Minimum in Rotated Sorted Array(二分)
- CodeForces - 697B Barnicle (高精度小数点)
- RuntimeBrowser查看iOS系统私有API
- Codeforces 396 div.2 带权并查集
- 变量的命名
- 通达OA jquery控制列表控件和单选框的实例代码
- C, C++混合编程通用Makefile模版
- POJ 1201 差分约束(链式前向星
- FPGA, 电路为什么要有触发器这种结构? 流水线
- YII相关资料(干货)
- GNU awk命令用法详解
- hdu3038带权并查集