Codeforces Round #396 (Div. 2)D. Mahmoud and a Dictionary(带权并查集)
来源:互联网 发布:淘宝卖家怎么注册帐号 编辑:程序博客网 时间:2024/05/21 09:42
题目链接:点击打开链接
思路:
带权并查集水题。 带权并查集可以知道在一个集合里的两点间距离。那么这种同义反义关心恰好对应距离的奇偶。
附上一图:
这就是合并的过程。
细节参见代码:
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <string>#include <vector>#include <stack>#include <ctime>#include <bitset>#include <cstdlib>#include <cmath>#include <set>#include <list>#include <deque>#include <map>#include <queue>#define Max(a,b) ((a)>(b)?(a):(b))#define Min(a,b) ((a)<(b)?(a):(b))using namespace std;typedef long long ll;typedef long double ld;const double eps = 1e-6;const double PI = acos(-1);const int mod = 1000000000 + 7;const int INF = 0x3f3f3f3f;// & 0x7FFFFFFFconst int seed = 131;const ll INF64 = ll(1e18);const int maxn = 1e5+10;int T,n,m,q,p[maxn],dist[maxn];map<string, int> mp;char s[33], s1[33];int _find(int x) { if(p[x] == x) return x; int oldfa = p[x]; p[x] = _find(p[x]); dist[x] = (dist[x] + dist[oldfa])%2; return p[x];}void init() { for(int i = 1; i <= n; i++) { p[i] = i; dist[i] = 0; }}int main() { scanf("%d%d%d", &n, &m, &q); for(int i = 1; i <= n; i++) { scanf("%s", s); mp[s] = i; } init(); for(int i = 1; i <= m; i++) { int id; scanf("%d%s%s", &id, s, s1); int id1 = mp[s], id2 = mp[s1]; int x = _find(id1), y = _find(id2); if(x != y) { printf("YES\n"); if(id == 1) p[x] = y, dist[x] = (dist[id2]-dist[id1]+2)%2; else p[x] = y, dist[x] = (dist[id2]-dist[id1]+1+2)%2; } else { int cur = (dist[id1]-dist[id2]+2)%2; if(id == 1) { if(cur & 1) printf("NO\n"); else printf("YES\n"); } else { if(cur & 1) printf("YES\n"); else printf("NO\n"); } } } while(q--) { scanf("%s%s", s, s1); int id1 = mp[s], id2 = mp[s1]; int x = _find(id1), y = _find(id2); if(x != y) printf("3\n"); else { int cur = (dist[id1] - dist[id2] + 2)%2; if(cur & 1) printf("2\n"); else printf("1\n"); } } return 0;}
0 0
- 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 (并查集)
- 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 #396 (Div. 2) D. Mahmoud and a Dictionary (并查集+map
- codeforces-766D-Mahmoud and a Dictionary(带权并查集)
- D. Mahmoud and a Dictionary----带权并查集
- codeforces 766D Mahmoud and a Dictionary(带权并查集+map查询)
- cf 766 d Mahmoud and a Dictionary(带权并查集)
- Codeforces Round #396 (Div. 2)Mahmoud and a Dictionary
- 【带权并查集】Mahmoud and a Dictionary
- 766D Mahmoud and a Dictionary[带权并查集][dfs]
- codeforce 766D Mahmoud and a Dictionary 带权并查集
- CodeForces - 766D Mahmoud and a Dictionary (并查集)
- 与JavaWeb开发相关的一些名词解释
- android使用高德地图SDK获取定位信息
- POJ - 2385 Apple Catching解题报告
- Java中的IO流
- 九度OJ-1109:连通图
- Codeforces Round #396 (Div. 2)D. Mahmoud and a Dictionary(带权并查集)
- HTTP Status 404 - There is no Action mapped for namespace [/] and action name [] associated with con
- 主成分分析-PCA
- Leetcode 97 - Interleaving String(dp)
- 运算电路基础
- 虚拟机ping不通主机,但是主机可以ping通虚拟机 ——linux笔记(1)
- 九度OJ-1445:How Many Tables
- Codeforces Round #321 (Div. 2)E 线段树+字符串hash
- 初学Android:广播机制学习小结