@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 ...
随机推荐
- 关于 Git 拉取GitLab工程报错:Repository not found的问题
[root@localhost xscan]# git pull fatal: repository 'http://gitlab.***.com/***.git/' not found 原因1: 可 ...
- PHP 调用qq邮箱接口
html代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...
- thymeleaf抛出项目上下文ServletContext ,session,request等信息
@RequestMapping("/alls") public String allsinfo(HttpSession session, HttpServletRequest re ...
- Mybatis配置-简单的使用
导包 基本配置 配置mybatis.config.xml文档 <?xml version="1.0" encoding="UTF-8" ?> < ...
- Spring_bean作用域
本篇介绍Spring Bean实例的作用范围,Spring Bean实例的作用范围由配置项scope限定.通过本篇的学习,可以达成如下目标. ● 应用scope配置项配置Bean的作用域 ● 应用单例 ...
- html浏览器高度和宽度和其他dom获取
1.获取网页可见区域的宽度:document.body.clientWidth ; 2.获取网页可见区域的高度:document.body.clientHeight; 3.获取 网页可见区域宽:doc ...
- Kubernetes学习笔记(八):Deployment--声明式的升级应用
概述 本文核心问题是:如何升级应用. 对于Pod的更新有两种策略: 一是删除全部旧Pod之后再创建新Pod.好处是,同一时间只会有一个版本的应用存在:缺点是,应用有一段时间不可用. 二是先创建新Pod ...
- 【Java8新特性】不了解Optional类,简历上别说你懂Java8!!
写在前面 最近,很多读者出去面试都在Java8上栽了跟头,事后自己分析,确实对Java8的新特性一知半解.然而,却在简历显眼的技能部分写着:熟练掌握Java8的各种新特性,能够迅速使用Java8开发高 ...
- 12 . Python3之网络编程
互联网的本质 两台计算机之间的通信与两个人打电话原理是一样的. # 1. 首先要通过各种物理连接介质连接 # 2. 找准确对方计算机(准确到软件)的位置 # 3. 通过统一的标准(一般子协议)进行数据 ...
- Bom和Dom对象
BOM-JavaScript是运行在浏览器中的,所以提供了一系列对象用于和浏览器窗口进行交互,这些对象主要包括window.document.location.navigator和screen等.通常 ...