【带权并查集】Mahmoud and a Dictionary
来源:互联网 发布:河南移动数据运营中心 编辑:程序博客网 时间:2024/05/21 09:29
#include <algorithm>#include <string>#include <map>#include <iostream>using namespace std;int *p, *g;int getset(int x){if (p[x] == x)return x;int y = getset(p[x]);g[x] = (g[p[x]] + g[x] + 2) % 2;return p[x] = y;}void merge(int z,int x, int y){z--;int xs = getset(x);int ys = getset(y);if (xs == ys){if ((g[x] - g[y] + 2) % 2 == z){cout << "YES" << endl;return;}else{cout << "NO" << endl;return;}}p[xs] = ys;g[xs] = (g[y] - g[x] + z + 2) % 2;cout << "YES" << endl;return;}int main(){int n, a, b;map<string, int> m;cin >> n >> a >> b;p = new int[n];g = new int[n];for (int i = 0; i < n; i++){p[i] = i;g[i] = 0;}for (int i = 0; i < n; i++){string tmp;cin >> tmp;m[tmp] = i;}string tmp1, tmp2;for (int i = 0; i < a; i++){int gx;cin >> gx >> tmp1 >> tmp2;merge(gx, m[tmp1], m[tmp2]);}for (int i = 0; i < b; i++){cin >> tmp1 >> tmp2;int x = getset(m[tmp1]);int y = getset(m[tmp2]);if (x == y)cout << (g[m[tmp1]] - g[m[tmp2]] + 2) % 2 + 1 << endl;else cout << 3 << endl;}//system("pause");return 0;}
阅读全文
0 0
- D. Mahmoud and a Dictionary----带权并查集
- 【带权并查集】Mahmoud and a Dictionary
- cf 766 d 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【带权并查集】
- 766D Mahmoud and a Dictionary[带权并查集][dfs]
- Codeforces Round #396 (Div. 2)D. Mahmoud and a Dictionary(带权并查集)
- codeforces-766D-Mahmoud and a Dictionary(带权并查集)
- codeforce 766D Mahmoud and a Dictionary 带权并查集
- codeforces766D Mahmoud and a Dictionary [并查集]【数据结构】
- 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(并查集)
- 并查集(Mahmoud and a Dictionary,cf 766D)
- CodeForces - 766D Mahmoud and a Dictionary (并查集)
- Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary (并查集)
- 从Activity跳到Fragment中并传参
- 安卓手机安装Linux教程 多图
- Nova中的扩展---学到hooks
- firewall-cmd的使用
- TabLayout (tabLayoout菜单)
- 【带权并查集】Mahmoud and a Dictionary
- ES6学习(二)——变量的解构赋值
- Linux电源管理(14)_从设备驱动的角度看电源管理
- Linux PM QoS framework(3)_per-device PM QoS
- 解决ionic请求数据跨域问题
- XListView上拉刷新,下拉加载
- Java程序员:工作还是游戏,是该好好衡量一下了
- 第一次使用树莓派会用到的操作(修改国内源,中文输入法,键盘布局,时区,挂载移动硬盘等)
- FAQ(9):org.apache.ibatis.binding.BindingException: Type interface com.smbms.dao.UserMapper is not kn