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

  1. Codeforces 669D Little Artem and Dance (胡搞 + 脑洞)

    题目链接: Codeforces 669D Little Artem and Dance 题目描述: 给一个从1到n的连续序列,有两种操作: 1:序列整体向后移动x个位置, 2:序列中相邻的奇偶位置互 ...

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

  3. CodeForces - 669D Little Artem and Dance 想法题 多余操作

    http://codeforces.com/problemset/problem/669/D 题意:n个数1~N围成一个圈.q个操作包括操作1:输入x, 所有数右移x.操作2:1,2位置上的数(swa ...

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

  5. codeforces 668C - Little Artem and Random Variable

    题目链接:http://codeforces.com/contest/668/problem/C --------------------------------------------------- ...

  6. codeforces 442C C. Artem and Array(有深度的模拟)

    题目 感谢JLGG的指导! 思路: //把数据转换成一条折线,发现有凸有凹 //有凹点,去掉并加上两边的最小值//无凹点,直接加上前(n-2)个的和(升序)//数据太大,要64位//判断凹与否,若一边 ...

  7. CodeForces 669E Little Artem and Time Machine

    树状数组,$map$. 可以理解为开一个数组$f[i][j]$记录:$i$这个数字在时间$j$的操作情况. 操作$1$:$f[x][t]++$.操作$2$:$f[x][t]--$.操作$3$:$f[x ...

  8. CodeForces 669D Little Artem and Dance

    模拟. 每个奇数走的步长都是一样的,每个偶数走的步长也是一样的. 记$num1$表示奇数走的步数,$num2$表示偶数走的步数.每次操作更新一下$num1$,$num2$.最后输出. #pragma ...

  9. CodeForces 669C Little Artem and Matrix GNU

    模拟. 把操作记录一下,倒着复原回去. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cs ...

随机推荐

  1. poj2594最小路径覆盖+floyd

    Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 8909   Accepted: 3 ...

  2. poj2391 最大流+拆点+二分答案+Floyd

    Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19553   Accepted: 4 ...

  3. Python中ThreadLocal的理解与使用

    一.对 ThreadLocal 的理解 ThreadLocal,有的人叫它线程本地变量,也有的人叫它线程本地存储,其实意思一样. ThreadLocal 在每一个变量中都会创建一个副本,每个线程都可以 ...

  4. python+selenium 自动化测试框架-学习记录

     本人小白一枚,想着把学习时的东西以博客的方式记录下来,文章中有不正确的地方请大佬多多指点!!共同学习 前期准备 安装python3.selenium.下载对应版本的webdriver:安装所需的第三 ...

  5. ExtJS定时和JS定时

    ExtJS定时 //定时刷新待办事宜状态 var task={ run:function(){ //执行的方法或方法体 }, interval:5*60*1000 //5分钟 } //定时启动 Ext ...

  6. vue 下拉列表动画

    点击可以收起,这里注意先给需要收起展开的的容器设置高度,通过样式v-enter和v-leave-to设置结束和开始前的就可以了

  7. sql中partition的使用

    https://www.cnblogs.com/tfiremeteor/p/6296599.html

  8. No grammar constraints (DTD or XML Schema) referenced in the document.的两种解决办法

    方法一:常用方法 关闭XML验证 工具栏:windows => preferences => xml => xml files => validation => Indi ...

  9. 面试题: SpringBoot 的自动配置原理

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 3.Spring Boot 的自动配置原理 package com.mmall; import org. ...

  10. 第九届蓝桥杯JavaC组省赛真题

    解题代码部分来自网友,如果有不对的地方,欢迎各位大佬评论 题目1.哪天返回 题目描述 小明被不明势力劫持.后被扔到x星站再无问津.小明得知每天都有飞船飞往地球,但需要108元的船票,而他却身无分文. ...