@codeforces - 668E@ Little Artem and 2-SAT
@description@
给定两个 2-sat 问题,询问两个问题的解集是否相同。
如果不相同,构造一组解 {xi},使得这个解是其中一个问题的解同时不是另一个问题的解。
@solution@
如果两者解集都为空,那么解集相同。
如果两者只有一个解集为空,则取另一个问题的任意解即可。
如果都有解,先跑个 bitset 优化的传递闭包,方便接下来的处理。
此时有一些变量的值是确定的(即可以从该变量的 0/1 状态到达该变量的 1/0 状态)。
如果有一个变量在问题 A 中确定而在问题 B 中不确定,则强制令 B 中该变量的值为 A 中值取反,暴力求此时 B 中的任意解。
如果有一个变量在两个问题中都确定但是确定的值不同,直接取问题 A 的任意解即可。
现在确定的变量都长一样了。但是不确定的变量可能相互制约,所以还是不能断定两个问题解集相同。
如果两个不确定的变量对应的结点 u, v,在问题 A 中有边相连 u -> v,在问题 B 中没有边相连,我们就可以构造出一组解。
在问题 B 中强制令 u 为真,v 为假,构造出 B 的任意解,该解在 A 中一定不成立。
@accepted code@
#include <cstdio>
#include <bitset>
#include <algorithm>
using namespace std;
const int MAXN = 2000;
bitset<MAXN>G[2][MAXN]; int n;
int abs(int x) {return x >= 0 ? x : -x;}
bool f[2]; int a[2][MAXN + 5];
void get(int o) {
for(int i=0;i<n;i++) {
if( a[o][i] == -1 ) {
for(int j=0;j<n;j++)
if( (a[o][j] == 0 && G[o][i<<1|1][j<<1|1]) || (a[o][j] == 1 && G[o][i<<1|1][j<<1]) ) {
a[o][i] = 0;
break;
}
if( a[o][i] == -1 ) a[o][i] = 1;
}
printf("%d ", a[o][i]);
}
exit(0);
}
int main() {
int m[2]; scanf("%d%d%d", &n, &m[0], &m[1]);
for(int o=0;o<2;o++) {
for(int i=1;i<=m[o];i++) {
int a, b; scanf("%d%d", &a, &b);
int p = (a < 0), q = (b < 0); a = abs(a) - 1, b = abs(b) - 1;
G[o][a<<1|p][b<<1|(!q)] = true, G[o][b<<1|q][a<<1|(!p)] = true;
}
for(int i=0;i<(n<<1);i++) G[o][i][i] = true;
for(int k=0;k<(n<<1);k++)
for(int i=0;i<(n<<1);i++)
if( G[o][i][k] ) G[o][i] |= G[o][k];
for(int i=0;i<n;i++) {
if( G[o][i<<1][i<<1|1] && G[o][i<<1|1][i<<1] ) {
f[o] = true;
break;
}
else if( G[o][i<<1][i<<1|1] ) a[o][i] = 1;
else if( G[o][i<<1|1][i<<1] ) a[o][i] = 0;
else a[o][i] = -1;
}
}
if( f[0] && f[1] ) puts("SIMILAR");
else if( f[0] ) get(1);
else if( f[1] ) get(0);
else {
for(int i=0;i<n;i++)
if( a[0][i] != a[1][i] ) {
if( a[0][i] == -1 ) a[0][i] = (!a[1][i]), get(0);
else if( a[1][i] == -1 ) a[1][i] = (!a[0][i]), get(1);
else get(0);
return 0;
}
for(int i=0;i<n;i++)
for(int j=0;j<n;j++) {
if( a[0][i] == -1 && a[0][j] == -1 ) {
if( G[0][i<<1|1][j<<1|1] != G[1][i<<1|1][j<<1|1] ) {
if( G[0][i<<1|1][j<<1|1] == 0 )
a[0][i] = 1, a[0][j] = 0, get(0);
else a[1][i] = 1, a[1][j] = 0, get(1);
return 0;
}
else if( G[0][i<<1|1][j<<1] != G[1][i<<1|1][j<<1] ) {
if( G[0][i<<1|1][j<<1] == 0 )
a[0][i] = 1, a[0][j] = 1, get(0);
else a[1][i] = 1, a[1][j] = 1, get(1);
return 0;
}
else if( G[0][i<<1][j<<1|1] != G[1][i<<1][j<<1|1] ) {
if( G[0][i<<1][j<<1|1] == 0 )
a[0][i] = 0, a[0][j] = 0, get(0);
else a[1][i] = 0, a[1][j] = 0, get(1);
return 0;
}
else if( G[0][i<<1][j<<1] != G[1][i<<1][j<<1] ) {
if( G[0][i<<1][j<<1] == 0 )
a[0][i] = 0, a[0][j] = 1, get(0);
else a[1][i] = 0, a[1][j] = 1, get(1);
return 0;
}
}
}
puts("SIMILAR");
}
}
@details@
注意有一些边界情况,预先给每个结点连个自环。
@codeforces - 668E@ Little Artem and 2-SAT的更多相关文章
- Codeforces 669D Little Artem and Dance (胡搞 + 脑洞)
题目链接: Codeforces 669D Little Artem and Dance 题目描述: 给一个从1到n的连续序列,有两种操作: 1:序列整体向后移动x个位置, 2:序列中相邻的奇偶位置互 ...
- codeforces 442C C. Artem and Array(贪心)
题目链接: C. Artem and Array time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- CodeForces - 669D Little Artem and Dance 想法题 多余操作
http://codeforces.com/problemset/problem/669/D 题意:n个数1~N围成一个圈.q个操作包括操作1:输入x, 所有数右移x.操作2:1,2位置上的数(swa ...
- CodeForces 668B Little Artem and Dance
B. Little Artem and Dance time limit per test 2 second memory limit per test 256 megabytes input sta ...
- codeforces 668C - Little Artem and Random Variable
题目链接:http://codeforces.com/contest/668/problem/C --------------------------------------------------- ...
- codeforces 442C C. Artem and Array(有深度的模拟)
题目 感谢JLGG的指导! 思路: //把数据转换成一条折线,发现有凸有凹 //有凹点,去掉并加上两边的最小值//无凹点,直接加上前(n-2)个的和(升序)//数据太大,要64位//判断凹与否,若一边 ...
- CodeForces 669E Little Artem and Time Machine
树状数组,$map$. 可以理解为开一个数组$f[i][j]$记录:$i$这个数字在时间$j$的操作情况. 操作$1$:$f[x][t]++$.操作$2$:$f[x][t]--$.操作$3$:$f[x ...
- CodeForces 669D Little Artem and Dance
模拟. 每个奇数走的步长都是一样的,每个偶数走的步长也是一样的. 记$num1$表示奇数走的步数,$num2$表示偶数走的步数.每次操作更新一下$num1$,$num2$.最后输出. #pragma ...
- CodeForces 669C Little Artem and Matrix GNU
模拟. 把操作记录一下,倒着复原回去. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cs ...
随机推荐
- SecureCRT VBscript连接指定端口和波特率
crt.Session.Connect "/Serial COM2 /BAUD 38400" 其它可用选项参考: crt.session.connect options https ...
- hdu6153KMP
A Secret Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)Total ...
- SpringCloud Alibaba 简介
SpringCloud Aliababa简介 SpringCloud Alibaba是阿里巴巴集团开源的一套微服务架构解决方案. 微服务架构是为了更好的分布式系统开发,将一个应用拆分成多个子应用,每一 ...
- 第4章 最基础的分类算法-k近邻算法
思想极度简单 应用数学知识少 效果好(缺点?) 可以解释机器学习算法使用过程中的很多细节问题 更完整的刻画机器学习应用的流程 distances = [] for x_train in X_train ...
- 四、Spring-面向切面编程
内容 面向切面编程基本原理 通过POJO创建切面 使用@AspectJ注解 为AspectJ切面注入依赖 关键词 横切关注点(cross-cutting concern) 继承 (inheritanc ...
- C#线程 基本同步
第二部分: 基本同步 同步要点 到目前为止,我们已经描述了如何在线程上启动任务,配置线程以及双向传递数据.我们还描述了局部变量如何专用于线程,以及如何在线程之间共享引用,从而允许它们通过公共字段进行 ...
- 前端开发SEO的理解
所谓seo(Search Engine Optimization)即搜索引擎优化.简单说就是百度.谷歌搜索引擎的‘蜘蛛’,如下图: 搜索引擎蜘蛛是通过,连接地址来找到你的网站的,seo就是让你的网站符 ...
- Alpha冲刺 —— 5.5
这个作业属于哪个课程 软件工程 这个作业要求在哪里 团队作业第五次--Alpha冲刺 这个作业的目标 Alpha冲刺 作业正文 正文 github链接 项目地址 其他参考文献 无 一.会议内容 1.展 ...
- Chisel3 - 模块
https://mp.weixin.qq.com/s/2vjM-gcauvHnn6KJzlOm4g Chisel的模块和Verilog的模块很相似,都用来定义模块结构(hierarchical s ...
- RocketMQ系列(一)基本概念
RocketMQ是阿里出品的一款开源的消息中间件,让其声名大噪的就是它的事务消息的功能.在企业中,消息中间件选择使用RocketMQ的还是挺多的,这一系列的文章都是针对RocketMQ的,咱们先从Ro ...