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. MySQ binlog三种模式

    MySQ binlog三种模式及设置方法 1.1 Row Level  行模式 日志中会记录每一行数据被修改的形式,然后在slave端再对相同的数据进行修改 优点:在row level模式下,bin- ...

  2. ◆◆◆◆◆◆◆◆◆◆◆linux下软件包的管理◆◆◆◆◆◆◆◆◆◆◆◆◆◆

    查看与制定的路径名相匹配的软件包 [root@localhost certs]# which ls alias ls='ls --color=auto' /bin/ls [root@localhost ...

  3. Mysql数据库迁移 Ubuntu14.04

    1. 停止数据库服务 sudo service mysql stop 2. 创建数据迁移目标文件夹(实际应该是挂载到新硬盘上) cd /var/lib ls -l drwx------  6 mysq ...

  4. 找回mysql数据库密码

    前提条件:你需要有数据库服务器的权限 1:修改my.ini配置文件 Mysqld:其中的d代表什么? Deamon后台运行的服务程序,增加一行跳过权限验证 2:停止mysql服务运行 3:启动mysq ...

  5. [DOM]有一种节点叫做文本节点

    HTML可以看成是由节点(node)组成的树结构 我们一般都是在<p>节点里面写字符串. 在上图中,<p>节点和字符串之间有一个text, 这个text就是文本节点. 我们可以 ...

  6. mysql table readonly

    if the table does not have primary key or unique non-nullable defined, then MySql workbench could no ...

  7. php 批量生成html,txt文件的方法(实例代码)

    php批量生成html,txt文件的实现代码. 首先,建立一个conn.php 链接数据库. <?php $link = mysql_connect("mysql_host" ...

  8. 在asp.net mvc中如何使用Grid++ Report (锐浪报表)

    在asp.net mvc中如何使用Grid++ Report (锐浪报表) 在cshtml,razor中的处理方法 以官方的asp.net(csharp)中的第一个示例"1a.简单表格&qu ...

  9. AWK 介绍

    一.模式和动作 awk脚本是由模式和操作组成的:pattern {action} pattern与{action}两者是可选的.如果没有模式,则action应用到全部记录,如果没有action,则输出 ...

  10. HTML中href的链接刷新页面问题

    在上一篇随笔中说到了html()方法不能一直改变标签的值的问题,当单击完成时,回调函数返回的值瞬间就没有了,今天突然想到了,我单击的是链接啊,就算链接到本界面上,也要进行刷新,页面一刷新,显示的值自然 ...