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的更多相关文章

  1. ORA-01102: cannot mount database in EXCLUSIVE mode

    安装完ORACEL 10g数据库后,启动数据库时遇到ORA-01102: cannot mount database in EXCLUSIVE mode [oracle@DB-Server ~]$ s ...

  2. Activiti之 Exclusive Gateway

    一.Exclusive Gateway Exclusive Gateway(也称为XOR网关或更多技术基于数据的排他网关)经常用做决定流程的流转方向.当流程到达该网关的时候,所有的流出序列流到按照已定 ...

  3. 启动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 ...

  4. informatica9.5.1资源库为machine in exclusive mode(REP_51821)

    错误信息: [PCSF_10007]Cannot connect to repository [Rs_RotKang] because [REP_51821]Repository Service is ...

  5. Delphi 异或,英文为exclusive OR,或缩写成xor

    异或,英文为exclusive OR,或缩写成xor 异或(xor)是一个数学运算符.它应用于逻辑运算.异或的数学符号为“⊕”,计算机符号为“xor”.其运算法则为: a⊕b = (¬a ∧ b) ∨ ...

  6. HDU 4919 Exclusive or (数论 or 打表找规律)

    Exclusive or 题目链接: http://acm.hust.edu.cn/vjudge/contest/121336#problem/J Description Given n, find ...

  7. 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 ...

  8. Shared and Exclusive Locks 共享和排它锁

    14.5 InnoDB Locking and Transaction Model InnoDB 锁和事务模型 14.5.1 InnoDB Locking 14.5.2 InnoDB Transact ...

  9. ORA-19573: cannot obtain exclusive enqueue for datafile 1

    还原Oracle数据库时出现ORA-19870和ORA-19573错误,如: RMAN> restore database; Starting restore at 11-DEC-12 usin ...

随机推荐

  1. 创建featureclass,为它赋别名,并移动到数据集下

    if (pOutFtrClass == null) { //continue; //创建featureclass //得到规范的字段集 IFields pFields = pFeatureClass. ...

  2. 多文件上传 iOS功能

    多文件上传 iOS功能,原文来自ios教程网整理的,大家可以看看演示:ios.662p.com ,喜欢的朋友可以看看我的博客吧. NSURL* url = [NSURL URLWithString:@ ...

  3. ARC forbids explicit message send of release

    http://blog.sina.com.cn/s/blog_7b9d64af01019rqt.html

  4. discuz random函数

    在研究邮箱非必填的过程中发现了个比较好用的random函数,在function_core.php中找到声明: function random($length, $numeric = 0) { $see ...

  5. PHP学习之环境搭建

    计算机环境 win7  64位 搭建  apache-httpd-2.2-win64  +  php-5.3.6-Win32-VC9-x64  +MySQL_5.5.13_winx64开发环境 参考: ...

  6. laravel扩展图片处理Intervention Image

    github地址:https://github.com/Intervention/image

  7. NULL, NUL, EOF, '\0',0区别

    NULL: 定义为0或0L或(void *)0,用于指示一个指针值是空,即什么都不指:'\0': 用且只用字符串结束符;NUL : 0x00,0值字符,可以用于结束ASCII字符串,和'\0'类似,但 ...

  8. C语言的32个关键字

    由ANSI标准定义的C语言关键字共个: auto double int struct break else long switch case enum register typedef char ex ...

  9. 1079. Total Sales of Supply Chain (25)

    时间限制 250 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A supply chain is a network of r ...

  10. 请给出一个左侧定宽右侧自适应的HTML结构及样式

    <!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...