Deduction Queries

用并查集维护前缀的关系, 在同一个联通块内两两之间的异或值都是已知的。

每个点再维护一个和它当前父亲的异或值, 压缩路径的时候更新一下就好了。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);using namespace std;const int N = 5e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}int q, tot;
map<int, int> Map;int xorWithFa[N];
int fa[N];int getRoot(int x) {if(x == fa[x]) return x;int preFa = fa[x];int nowFa = getRoot(fa[x]);xorWithFa[x] ^= xorWithFa[preFa];return fa[x] = nowFa;
}int main() {for(int i = 1; i < N; i++) fa[i] = i;scanf("%d", &q);int ans = 0;while(q--) {int op; scanf("%d", &op);if(op == 1) {int l, r, x;scanf("%d%d%d", &l, &r, &x);l ^= ans; r ^= ans; x ^= ans;if(l > r) swap(l, r);l--;if(Map.find(l) == Map.end()) Map[l] = ++tot;if(Map.find(r) == Map.end()) Map[r] = ++tot;l = Map[l]; r = Map[r];int fal = getRoot(l);int far = getRoot(r);if(fal != far) {int xor1 = xorWithFa[l];int xor2 = xorWithFa[r];fa[far] = fal;xorWithFa[far] = xor1 ^ xor2 ^ x;}} else {int l, r;scanf("%d%d", &l, &r);l ^= ans; r ^= ans;if(l > r) swap(l, r);l--;if(Map.find(l) == Map.end() || Map.find(r) == Map.end()) {puts("-1");ans = 1;} else {l = Map[l]; r = Map[r];int fal = getRoot(l);int far = getRoot(r);if(fal != far) {puts("-1");ans = 1;} else {ans = xorWithFa[l] ^ xorWithFa[r];printf("%d\n", ans);}}}}return 0;
}/*
*/

转载于:https://www.cnblogs.com/CJLHY/p/10875235.html

Codeforces 1144D Deduction Queries 并查集相关推荐

  1. CodeForces - 123A prime permutation(并查集,水题)

    题目链接:点击查看 题目大意:给出一个字符串s,问能否通过重组其字母顺序,从而满足:若字符串下标从1开始,对于每一个质数下标,满足 题目分析:其实在纸上稍微写写画画就能看出个大概的规律,当字符串的长度 ...

  2. CodeForces - 468B Two Sets(并查集+思维)

    题目链接:点击查看 题目大意:现在给出两个集合A和B,再给出两个数a和b,现在规定在集合A中的数x必须满足x和a-x同时在集合a中,而在集合B中的数x也同样需要满足x和b-x同时在集合B中,现在给出一 ...

  3. Harmonious Graph CodeForces - 1253D(思维+并查集)

    You're given an undirected graph with n nodes and m edges. Nodes are numbered from 1 to n. The graph ...

  4. codeforces 884E Binary Matrix 并查集,滚动数组

    E. Binary Matrix time limit per test 3 seconds memory limit per test 16 megabytes input standard inp ...

  5. Codeforces 371D. Vessels【并查集】

    题目大意: 给出一堆从上到下叠起来的容器,有两种操作:第一种是在编号为i的容器中加入x的水(保证水溢出之后会流到下一个最近的容器(如果存在)中):第二种是查询编号为i的容器中有多少水. 做法: 首先, ...

  6. CodeForces - 371D Vessels 【并查集】

    Vessels 题意: 自上而下的n个碗,向某个碗中倒水,如果溢出,则会流向它之下的下一个未满的碗.有两种操作:1 p x表示往第p个碗中导入x的水,2 p表示询问此时第p个碗中的水量. 题解: 未经 ...

  7. CodeForces - 371D. Vessels(并查集)

    题目链接:http://codeforces.com/problemset/problem/371/D点击打开链接 D. Vessels time limit per test 2 seconds m ...

  8. CodeForces - 1217F Forced Online Queries Problem(线段树分治+并查集撤销)

    题目链接:点击查看 题目大意:给出 nnn 个点,初始时互相不存在连边,需要执行 mmm 次操作,每次操作分为两种类型: 1xy1 \ x \ y1 x y:如果 (x,y)(x,y)(x,y) 之间 ...

  9. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C. String Reconstruction 并查集

    C. String Reconstruction 题目连接: http://codeforces.com/contest/828/problem/C Description Ivan had stri ...

最新文章

  1. Oral English 1: A topic about public transportaion in Chicago.
  2. Infragistics NetAdvantage控件的使用:该关系不是此 DataView 指向的表的父关系
  3. page rank算法
  4. vue项目:this.function()中关于:this指针失效的问题
  5. dede index.php权限_织梦实现全站动态,限制只有会员才能查看
  6. OpenSTA —— 高效的 web 性能测试工具
  7. 关于System.identityHashCode(obj) 与 obj.hashcode()
  8. Vbox中Ubuntu和win7主机文件共享
  9. JVM第十二章-垃圾回收器
  10. Java实现打印日历
  11. C语言:关键字---struct(声明结构体类型)
  12. Java分布式跟踪系统Zipkin(六):Brave源码分析-Brave和SpringBoot整合
  13. amd超频软件LINUX,AMD官方超频工具下载_AMD OverDrive 超频工具-PChome下载中心
  14. linux安装opencv,在安装了两天后终于安装上了。坚持就是胜利。
  15. 罗马音平假字复制_五十音图的那些平假字和片假字有罗马音吗?
  16. 得之坦然,失之淡然,顺其自然,争其必然
  17. 网易云课堂学习-TCP/UDP协议
  18. 学生党超实用的在线gpu加速服务器ATUODL,与pycharm连接使用教程。
  19. 小哥自创AI防拖延系统,一玩手机就被“闪瞎” | Reddit高热
  20. win7本地连接状态信息空白的解决方法

热门文章

  1. ASP.NET 2.0 的数据源、数据绑定控件概述与区别
  2. jq获取input选取的文件名_tushare获取交易数据并可视化分析
  3. android 坐标点计算器,Android实现简易计算器
  4. matlabapp窗口图像_matlab – 如何自定义App Designer图形的背景?
  5. redhat linux7.0的安装
  6. C++控制向文件中写入浮点数的格式
  7. php标签嵌入规范,HTML标签嵌套的详细规则
  8. 160 - 9 Andrnalin.2
  9. json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
  10. 【嵌入式系统复习】嵌入式网络与协议栈