http://codeforces.com/contest/766/problem/D

所谓种类并查集,题型一般如下:给定一些基本信息给你,然后又给出一些信息,要求你判断是真是假。例如给出a和b支持不同的队伍,而且b和c也是支持不同的队伍,由于队伍只有两支(就是说只有两种),所以可以推出a和c是支持同一个队伍。

你可能会想用两个并查集,一个并查集存放一个队伍。但是这样是不行的,十分麻烦。因为你想想,如果给出[a,b]不同,然后[c,d]不同,如果我按照左边的放在同一个集合,那么我接着[a,c]不同,这样就会是(a,d)相同,这样的话,你要更改那个并查集,是十分麻烦的。

正解:只用一个并查集,而且再维护一个数组rank[i]表示i与father的关系,0表示支持同一个球队,1表示不同,这样的话,就可以根据rank[x]==rank[y]来判断是不是支持相同的了。爸爸支持谁没所谓啊,我们不关心支持哪个球队,我们只关心支持的是否一样罢了。rank[]数组压缩路径和并查集一样的,只不过其中要列些数据,推些公式出来。

大致思路和食物链那些题目差不多。

设rex[i]表示i号顶点和爸爸的关系是什么

0,表示同一类型,1表示不同类型。

然后细心推个公式,就行了

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 1e5 + ;
int fa[maxn], rex[maxn];
map<string, int>mp;
int tofind(int u) {
if (fa[u] == u) {
rex[u] = ;
return u;
}
int t = fa[u];
fa[u] = tofind(fa[u]);
rex[u] = (rex[u] + rex[t]) % ;
return fa[u];
}
int tomerge(int x, int y, int val, int is) {
int tx = x, ty = y;
x = tofind(x);
y = tofind(y);
if (is) {
if (x != y) return ;
if (rex[tx] != rex[ty]) {
return ;
} else return ;
}
if (x != y) {
fa[y] = x;
rex[x] = ;
rex[y] = (rex[ty] + rex[tx] + val) % ;
return ;
}
return !((rex[tx] + rex[ty] + val) % );
}
void work() {
int n, m, q;
cin >> n >> m >> q;
for (int i = ; i <= n; ++i) {
string s;
cin >> s;
mp[s] = i;
fa[i] = i;
}
for (int i = ; i <= m; ++i) {
int id;
string s1, s2;
cin >> id >> s1 >> s2;
id--;
if (tomerge(mp[s1], mp[s2], id, )) {
cout << "YES" << endl;
} else cout << "NO" << endl;
}
for (int i = ; i <= q; ++i) {
string s1, s2;
cin >> s1 >> s2;
cout << tomerge(mp[s1], mp[s2], , ) << endl;
}
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
IOS;
work();
return ;
}

D. Mahmoud and a Dictionary 种类并查集的更多相关文章

  1. codeforces#766 D. Mahmoud and a Dictionary (并查集)

    题意:给出n个单词,m条关系,q个询问,每个对应关系有,a和b是同义词,a和b是反义词,如果对应关系无法成立就输出no,并且忽视这个关系,如果可以成立则加入这个约束,并且输出yes.每次询问两个单词的 ...

  2. 【CF766D】Mahmoud and a Dictionary(并查集)

    题意:有n个单词,给定m个关系,每个关系要么表示单词a与单词b相同,要么表示单词a与单词b相反. 并且“相同”与“相反”有性质:若a与b相同,b与c相同,则a与c相同(从而单词的相同关系是等价关系): ...

  3. codeforces 766 D. Mahmoud and a Dictionary(种类并查集+stl)

    题目链接:http://codeforces.com/contest/766/problem/D 题意:给你n个单词,m个关系(两个单词是反义词还是同义词),然后问你所给的关系里面有没有错的,最后再给 ...

  4. NOI2001|POJ1182食物链[种类并查集 向量]

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 65430   Accepted: 19283 Description ...

  5. NOIP2010关押罪犯[并查集|二分答案+二分图染色 | 种类并查集]

    题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用“怨气值”(一个正整数值)来表示 ...

  6. POJ1703Find them, Catch them[种类并查集]

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42416   Accepted: ...

  7. poj1417(种类并查集+dp)

    题目:http://poj.org/problem?id=1417 题意:输入三个数m, p, q 分别表示接下来的输入行数,天使数目,恶魔数目: 接下来m行输入形如x, y, ch,ch为yes表示 ...

  8. poj1733(种类并查集+离散化)

    题目链接: http://poj.org/problem?id=1733 题意: 输入n表示有一个长度为n的0,1字符串, m表示接下来有m行输入, 接下来的m行输入中x, y, even表示第x到第 ...

  9. poj 1182:食物链(种类并查集,食物链问题)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44168   Accepted: 12878 Description ...

随机推荐

  1. Hive中行列转换

    1.演示多列转为单行 数据文件及内容: student.txt xiaoming|english|92.0 xiaoming|chinese|98.0 xiaoming|math|89.5 huahu ...

  2. GLib-GObject-WARNING **: Two different plugins tried to register

    使用 Ubuntu vivid 的 glib-2.44.1 的程序总是打印类似这种警告: GLib-GObject-WARNING **: Two different plugins tried to ...

  3. Java线程池的简单使用

    最近由于公司的业务需求,需要使用线程池来进行对数据进行处理,所以就简单的学习了一下线程池的东西,刚接触感觉挺难的,不过使用了就不感觉那么难了,还是蛮简单的, package com.yd.sms.jo ...

  4. Redis管理各类型存储数据命令

    >>>字符串 1 SET key value 设置指定 key 的值 2 GET key 获取指定 key 的值. 3 GETRANGE key start end 返回 key 中 ...

  5. 2016/2/29 html 思维导图

  6. 反爬统计 数据库 sql CASE

    -- 经排查日志,发现ordertest.com下的url检测,频繁<Response [403]>,Forbidden;再进一步查询数据库数据:逐日统计错误临时表test_error_t ...

  7. Bootstrap + Font Awesome

    将Font Awesome 集成到 Bootstrap 非常容易,还可以被单独使用. 最简单的 Bootstrap + Font Awesome 集成方式 使用这种方式将 Font Awesome 集 ...

  8. ABAP 读取FTP文件

    VALUE '172.168.1.250'. VALUE 'username'. VALUE 'password'. ."密钥 CONSTANTS: cns_rfcdest LIKE rfc ...

  9. 工作中常用到的JS校验

    1. // 验证是否为空 2. function check_blank(obj, obj_name){ 3. if(obj.value != ''){ 4. return true; 5. }els ...

  10. [Usaco2015 DEC] Counting Haybales

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4392 [算法] 线段树 时间复杂度 : O(MlogN) [代码] #include ...