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 ...
随机推荐
- What are Upgrade, Product and Package Codes used for? By pusu
Following content is reprinted from here, please go to the original website for more information. Au ...
- VS代码模板
Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\Csharp\Code\2052\Class
- xamarin android——数据绑定到控件(二)
本示例为通过媒体内容提供器获取本机中的图片显示在Gallery中. 活动中简单的初始化代码 private void InitGallery() { Gallery gallery = FindVie ...
- DEDECMS中,arclist标签
文档列表 dede:arclist 标签: {dede:arclist flag='h' typeid='' row='' col='' titlelen='' infolen='' imgwidt ...
- 关于maven参数过滤
一.maven通过设置过滤器,可以使maven在编译打包时实现参数过滤的功能(详细配置说明略) <filters> <filter>../antx.properties< ...
- Python for 循环 失效
昨天发现一个负责处理观察者模式的基类工作失败,默认的N个观察者负责处理 发送的一些东西, 其中提供一个内置接口移除观察者: def removeObserver(self, observer): if ...
- [译] ASP.NET 生命周期 – ASP.NET 上下文对象(六)
使用 HttpApplication 对象 ASP.NET 框架中的许多类都提供了许多很方便的属性可以直接映射到 HttpContext 类中定义的属性.这种交叠有一个很好的例子就是 HttpAppl ...
- 【BZOJ 1934】 [Shoi2007]Vote 善意的投票
Description 幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神.虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法,他们也可 ...
- mysql日志详细解析 [转]
原文出处:http://pangge.blog.51cto.com/6013757/1319304 MySQL日志: 主要包含:错误日志.查询日志.慢查询日志.事务日志.二进制日志: 日志是mysql ...
- python学习笔记21(正则表达式)
正则表达式模式: 模式 描述 ^ 匹配的开始的 $ 匹配行尾 . 匹配除换行符的任何单个字符.使用-m选项允许其匹配换行符也是如此. [...] 匹配括号内任何单个字符 [^...] 匹配非单个字符集 ...