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 ;
}

最新文章

  1. Android中的动画效果
  2. 解决PHP move_uploaded_file函数移动图片失败
  3. poj2318
  4. String、StringBuffer、StringBuilder源码分析
  5. POJ 2891 Strange Way to Express Integers(拓展欧几里得)
  6. TListView Header重绘和高度设置
  7. Redis学习手册(Sorted-Sets数据类型)
  8. UVa 103 Stacking Boxes --- DAG上的动态规划
  9. Redis集群搭建&amp;访问
  10. Ax Grid 的显示根据用户的需求动态排序。
  11. UX结合需求实例化进行设计开发
  12. 部署 instance 到 OVS flat network - 每天5分钟玩转 OpenStack(135)
  13. Swift 3必看:新的访问控制fileprivate和open
  14. PHPExcel 导出
  15. Http协议学习总结(转)
  16. Xcode 5.1.1 与 Xcode 6.0.1 共存
  17. Spring+SpringMVC+MyBatis深入学习及搭建(十五)——SpringMVC注解开发(基础篇)
  18. Redis Codis 部署安装
  19. 【Spring】26、利用Spring的AbstractRoutingDataSource解决多数据源,读写分离问题
  20. [转]01分数规划算法 ACM 二分 Dinkelbach 最优比率生成树 最优比率环

热门文章

  1. springMVC4(16)拦截器解析与登陆拦截模拟
  2. Zookeeper 3.4 官方文档翻译
  3. javascript/jquery模板引擎——Handlebars初体验
  4. Exception from container-launch: org.apache.hadoop.util.Shell$ExitCodeException
  5. 通过通过url routing解决UIViewController跳转依赖
  6. [Spring实战系列](19)Servlet不同版本号之间的差别
  7. 常见网络摄像机默认使用的端口,RTSP地址
  8. SQL Server 存储过程具体解释
  9. Java小白手记2:一些名词解释
  10. xcode10的那些事