【HDOJ】3234 Exclusive-OR
并查集。对于对元素赋值操作,更改为I p n v。令val[n]=0(任何数与0异或仍为原值)。
考虑fa[x] = fx, fa[y] = fy。
如果使得fa[fx] = fy, 那么val[fx] = TrueVal[fx] ^ TrueVal[fy] = (val[x] ^ TrueVal[x]) ^ (val[y] ^ TrueVal[y]) = (val[x] ^ val[y]) ^ (TrueVal[x] ^ TrueVal[y]) = val[x] ^ val[y] ^ z。
同时每次find时,也需要更新val(因为fa不同了)。
对于Query操作,有效的Query为cnt为偶数或者root为n。
输出样例有问题,每个case后有空行。
/* 3234 */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = 2e4+;
const int maxk = ;
int fa[maxn], val[maxn];
int P[maxk], K;
bool visit[maxk];
int n; int find(int x) {
if (fa[x] == x)
return x;
int tmp = fa[x];
fa[x] = find(fa[x]);
val[x] ^= val[tmp];
return fa[x];
} void init() {
rep(i, , n+) {
val[i] = ;
fa[i] = i;
}
} bool merge(int u, int v, int x) {
int fu = find(u);
int fv = find(v); if (fu == fv) {
return (val[u]^val[v]) == x;
} if (fu == n) swap(fu, fv);
fa[fu] = fv;
val[fu] = val[u]^val[v]^x;
return true;
} int Query() {
int f, c;
int ret = ; memset(visit, false, sizeof(visit));
rep(i, , K)
find(P[i]);
rep(i, , K) {
if (visit[i])
continue;
f = fa[P[i]];
c = ;
rep(j, i, K) {
if (!visit[j] && fa[P[j]]==f) {
++c;
visit[j] = true;
ret ^= val[P[j]];
}
}
if (f!=n && (c&))
return -;
} return ret;
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int q;
int isn;
int a[], c;
int u, v, x;
int ans;
char cmd[], line[];
bool flag;
int t = ; while (scanf("%d %d", &n, &q)!=EOF && (n||q)) {
printf("Case %d:\n", ++t);
init();
flag = true;
isn = ;
while (q--) {
scanf("%s%*c", cmd);
if (cmd[] == 'I') {
gets(line);
} else {
scanf("%d", &K);
rep(i, , K)
scanf("%d", &P[i]);
}
if (!flag)
continue; if (cmd[] == 'I') {
int len = strlen(line); ++isn;
c = ;
a[c] = ;
rep(i, , len) {
if (line[i] == ' ') {
++c;
a[c] = ;
} else {
a[c] = *a[c] + line[i]-'';
}
} if (c == ) {
u = a[];
v = n;
x = a[];
} else {
u = a[];
v = a[];
x = a[];
} flag = merge(u, v, x); if (!flag) {
printf("The first %d facts are conflicting.\n", isn);
}
} else {
ans = Query();
if (ans == -) {
puts("I don't know.");
} else {
printf("%d\n", ans);
}
}
} putchar('\n');
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
【HDOJ】3234 Exclusive-OR的更多相关文章
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 【HDOJ】【3516】Tree Construction
DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...
- 【HDOJ】【3480】Division
DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...
- 【HDOJ】【2829】Lawrence
DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...
- 【HDOJ】【3415】Max Sum of Max-K-sub-sequence
DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...
- 【HDOJ】【3530】Subsequence
DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...
- 【HDOJ】【3068】最长回文
Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...
- 【HDOJ】【1512】Monkey King
数据结构/可并堆 啊……换换脑子就看了看数据结构……看了一下左偏树和斜堆,鉴于左偏树不像斜堆可能退化就写了个左偏树. 左偏树介绍:http://www.cnblogs.com/crazyac/arti ...
随机推荐
- Touch事件or手机卫士面试题整理回答(二)
Touch事件or手机卫士面试题整理回答(二) 自定义控件 1. Touch事件的传递机制 顶级View->父View->子View,不处理逆向返回 OnInterceptTouchEve ...
- 解决OOM小记
跟猜想的一样是OOM.一回来遇一不怎么熟悉的sb,给我气的....算了.....哥哥也是种种原因回的合肥.继续看问题. 这个地方的界面是这样的 划红线的地方是三个LinearLayout,每次oncl ...
- (转)C#中的Dictionary字典类介绍
关键字:C# Dictionary 字典 作者:txw1958原文:http://www.cnblogs.com/txw1958/archive/2012/11/07/csharp-dictionar ...
- ios NSMethodSignature and NSInvocation 消息转发
1.首先获取消息转发时连个函数内部具体内容 MARK:这里是拿[@"xxxxx" length]调用拿来举例说明 (lldb) po signature <NSMethodS ...
- iOS9 http不能访问网络——在Xcode中将https改成http方式
=====================2016-01-29更新=========================== 最近做demo时,发现将https改成http方式略有小变 1. 没有改成ht ...
- java新手笔记20 抽象类模板(letter)
1.抽象类 package com.yfs.javase; //信模板 public abstract class Templater { public abstract String toName( ...
- [翻译][MVC 5 + EF 6] 2:基础的增删改查(CRUD)
原文:Implementing Basic CRUD Functionality with the Entity Framework in ASP.NET MVC Application 1.修改Vi ...
- 隐性改变display类型
有一个有趣的现象就是当为元素(不论之前是什么类型元素,display:none 除外)设置以下 2 个句之一: position : absolutefloat : left 或 float:righ ...
- varnish 4.0编译安装小记
varnish 4.0 编译问题 centos-6.5 x86环境 装varnish遇到几个错误要先安装python-docutils然后提示error1,于是安装:libedit-devel然后提示 ...
- MySQL的基本
MySQL的基本语法 left JOIN 左表匹配右表 有没有内容全部匹配 SELECT Persons.LastName, Orders.OrderNo FROM Persons INNER JOI ...