12232 - Exclusive-OR
题目大意是可以设定一个点Xp=v,或者Xp^Xq=v,然后查询Xa^Xb^Xc...等于多少。
由于异或操作跟判连通性很类似,这里可以使用并查集来解决,对于Xp^Xq=v先判断Xp和Xq是否来自一颗树,若是来自一个棵树,则判相容性,否则连接这两棵树,而Xp=v是用来lock一棵树的,当一棵树未被lock时,这颗树中的所有值都不确定,当lock之后,就可以确定这颗树的所有值了。而最后求Xa^Xb^Xc...时,先按树进行分类,对于lock的树的X直接求解,都与非lock的树,若有偶数个则可以求解,否则得不到固定的值,算法应该没有问题,可是AC不了,不好debug,下面是代码:
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <sstream>
using namespace std; const int MAXN = ; int f[MAXN], p[MAXN], xor_value[MAXN]; int find_father(int i) {
if (f[i] == i) {
xor_value[i] = ;
return i;
} else {
int root = find_father(f[i]);
xor_value[i] ^= xor_value[f[i]];
return f[i] = root;
}
} int insert(vector<int> v) {
if (v.size() == ) {
int root = find_father(v[]);
if (p[root] == - || p[root] == (xor_value[v[]] ^ v[])) {
p[root] = xor_value[v[]] ^ v[];
} else {
return -;
}
} else {
int root0 = find_father(v[]);
int root1 = find_father(v[]);
if (root0 == root1) {
if ((xor_value[v[]] ^ xor_value[v[]]) != v[]) {
return -;
}
} else {
f[root1] = root0;
xor_value[root1] = xor_value[v[]] ^ xor_value[v[]] ^ v[];
if (p[root1] != -) {
int tmp = p[root1] ^ xor_value[root1];
if (p[root0] == - || p[root0] == tmp) {
p[root0] = tmp;
} else {
return -;
}
}
}
}
return -;
} int query(vector<int> v) {
map<int, vector<int> > kind;
for (int i = ; i < v.size(); i++) {
vector<int> zero;
int father = find_father(v[i]);
if (kind.find(father) == kind.end()) {
kind[father] = zero;
}
kind[father].push_back(v[i]);
}
int res = ;
for (map<int, vector<int> >::iterator it = kind.begin(); it != kind.end(); it++) {
vector<int> son = it->second;
if (p[it->first] != -) {
for (int i = ; i < son.size(); i++) {
res ^= (p[it->first] ^ xor_value[son[i]]);
}
} else {
if (son.size() & ) {
return -;
} else {
for (int i = ; i < son.size(); i += ) {
res ^= (xor_value[son[i]] ^ xor_value[son[i + ]]);
}
}
}
}
return res;
} int main() {
int n, q, cc = ;
while (scanf("%d%d", &n, &q)) {
if (n == && q == ) break;
getchar();
printf("Case %d:\n", ++cc);
for (int i = ; i < n; i++) f[i] = i;
memset(p, -, sizeof(p));
int facts = ;
bool bad = false;
for (int c = ; c < q; c++) {
int t;
char arg[], cmd;
vector<int> v; gets(arg); if (bad) continue; stringstream ss(arg); ss >> cmd;
while (ss >> t) v.push_back(t);
int res;
if (cmd == 'I') {
res = insert(v);
facts++;
} else if (cmd == 'Q') {
res = query(v);
}
if (res == -) printf("I don't konw.\n");
else if (res == -) {
printf("The first %d facts are conflicting.\n", facts);
bad = true;
} else if (res >= ) printf("%d\n", res);
}
printf("\n");
}
}
12232 - Exclusive-OR的更多相关文章
- ORA-01102: cannot mount database in EXCLUSIVE mode
安装完ORACEL 10g数据库后,启动数据库时遇到ORA-01102: cannot mount database in EXCLUSIVE mode [oracle@DB-Server ~]$ s ...
- Activiti之 Exclusive Gateway
一.Exclusive Gateway Exclusive Gateway(也称为XOR网关或更多技术基于数据的排他网关)经常用做决定流程的流转方向.当流程到达该网关的时候,所有的流出序列流到按照已定 ...
- 启动weblogic的错误:Could not obtain an exclusive lock to the embedded LDAP data files directory
http://hi.baidu.com/kaisep/item/0e4bf6ee5da001d1ea34c986 源地址 启动weblogic的错误:Could not obtain an exclu ...
- informatica9.5.1资源库为machine in exclusive mode(REP_51821)
错误信息: [PCSF_10007]Cannot connect to repository [Rs_RotKang] because [REP_51821]Repository Service is ...
- Delphi 异或,英文为exclusive OR,或缩写成xor
异或,英文为exclusive OR,或缩写成xor 异或(xor)是一个数学运算符.它应用于逻辑运算.异或的数学符号为“⊕”,计算机符号为“xor”.其运算法则为: a⊕b = (¬a ∧ b) ∨ ...
- HDU 4919 Exclusive or (数论 or 打表找规律)
Exclusive or 题目链接: http://acm.hust.edu.cn/vjudge/contest/121336#problem/J Description Given n, find ...
- If one session has a shared or exclusive lock on record R in an index, another session cannot insert
If one session has a shared or exclusive lock on record R in an index, another session cannot insert ...
- Shared and Exclusive Locks 共享和排它锁
14.5 InnoDB Locking and Transaction Model InnoDB 锁和事务模型 14.5.1 InnoDB Locking 14.5.2 InnoDB Transact ...
- ORA-19573: cannot obtain exclusive enqueue for datafile 1
还原Oracle数据库时出现ORA-19870和ORA-19573错误,如: RMAN> restore database; Starting restore at 11-DEC-12 usin ...
随机推荐
- C++记录2
1, 求成员变量的偏移: 2, const实现机制:在编译期间完成,对于内置类型,如int, 编译器可能使用常数直接替换掉对此变量的引用.而对于结构体不一定. 编译器在优化代码时把j直接优化成64h了 ...
- Hadoop示例程序WordCount编译运行
首先确保Hadoop已正确安装及运行. 将WordCount.java拷贝出来 $ cp ./src/examples/org/apache/hadoop/examples/WordCount.jav ...
- 完美高仿精仿京东商城手机客户端android版源码
完美高仿精仿京东商城手机客户端android版源码,是从安卓教程网那边转载过来的,这款应用源码非常不错的,也是一个非常优秀的应用源码的,希望能够帮到学习的朋友. _js_op> <igno ...
- 锋利的jquery-validation
jquery插件 jquery插件项目托管于gitHub,项目地址https://github.com/jquery/plugins.jquery.com jquery插件的使用 表单验证插件 现在网 ...
- 把div固定在网页顶部
很多网站都有把某一块固定在顶部的功能,今天上网搜搜然后自己又写了一遍,贴出来给大家看看,哪天用到的时候自己也可以随时看看 <!DOCTYPE html PUBLIC "-//W3C// ...
- artdialog 提示 确定或取消
dialog({ title:'提示', content:"下载需扣除" + point + "个积分<br />重复下载不扣积分,需要继续吗?", ...
- Scrapy简介
什么是Scrapy? Scrapy是一个快速.高级的爬行器和网页抓取框架,用来抓取网站和提取网页中结构化的数据.它被广泛的使用于监控数据采集和自动化测试. 参考:http://scrapy.org/
- Oracle Lock 概述
按锁的机制分类 排他锁( X ):如果事务T对对象A加上排他锁,则只允许T对A对象读取和修改,其他事务不能对A增加任何锁,直到T释放加载A上的排他锁 共享锁( S ):如果事务T对表A加上共享锁,则事 ...
- MySQL 5.6.21 最新版的安装
上一篇 写了对于入门者来说困难的事情,博主最近装了mysql软件,下面来看看mysql的安装. 下载mysql软件安装程序,我的是在百度软件中心上下载的,选择百度是因为里面没有什么捆绑软件: 下载完了 ...
- linux下使用sqlplus使用上下键显示历史命令
在linux下使用sqlplus没有在windows下方便,既不好用习惯的退格键,也无法通过上键来显示上一条的命令,这里我们使用rlwrap来解决. 1.安装rlwrap [root@toughhou ...