几个关于2-sat的题

HDU3062

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3062

题意:

从2n个人去宴会,有 m条关系 i和j不能同时去 夫妻不能同时去 问能否有n个人出席

题解:

因为是n对夫妻,我们将编号扩展 奇数是丈夫,偶数是妻子

那么m条关系就是 i*2+a 和j*2+b不能同时出席

建边 选b必选 a‘

选a必选b'

然后判断是否在一个强连通分量里面即可

代码:

/**
*        ┏┓    ┏┓
*        ┏┛┗━━━━━━━┛┗━━━┓
*        ┃       ┃  
*        ┃   ━    ┃
*        ┃ >   < ┃
*        ┃       ┃
*        ┃... ⌒ ...  ┃
*        ┃       ┃
*        ┗━┓   ┏━┛
*          ┃   ┃ Code is far away from bug with the animal protecting          
*          ┃   ┃   神兽保佑,代码无bug
*          ┃   ┃           
*          ┃   ┃       
*          ┃   ┃
*          ┃   ┃           
*          ┃   ┗━━━┓
*          ┃       ┣┓
*          ┃       ┏┛
*          ┗┓┓┏━┳┓┏┛
*           ┃┫┫ ┃┫┫
*           ┗┻┛ ┗┻┛
*/
// warm heart, wagging tail,and a smile just for you!
//
//                           _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                           O\ = /O
//                       ____/`---'\____
//                     .' \|     |// `.
//                     / \||| : |||// \
//                   / _||||| -:- |||||- \
//                   |   | \ - /// |   |
//                   | \_| ''\---/'' |   |
//                   \ .-\__ `-` ___/-. /
//                 ___`. .' /--.--\ `. . __
//               ."" '< `.___\_<|>_/___.' >'"".
//             | | : `- \`.;`\ _ /`;.`/ - ` : | |
//             \ \ `-.   \_ __\ /__ _/   .-` / /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                           `=---='
//       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//                     佛祖保佑     永无BUG
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 2e3 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double Pi = acos(-1);
LL gcd(LL a, LL b) {
   return b ? gcd(b, a % b) : a;
}
LL lcm(LL a, LL b) {
   return a / gcd(a, b) * b;
}
double dpow(double a, LL b) {
   double ans = 1.0;
   while(b) {
       if(b % 2)ans = ans * a;
       a = a * a;
       b /= 2;
  } return ans;
}
LL quick_pow(LL x, LL y) {
   LL ans = 1;
   while(y) {
       if(y & 1) {
           ans = ans * x % mod;
      } x = x * x % mod;
       y >>= 1;
  } return ans;
}
struct EDGE {
   int v, nxt;
} edge[maxn * maxn];
int n, m;
int head[maxn], tot;
void add_edge(int u, int v) {
   edge[tot].v = v;
   edge[tot].nxt = head[u];
   head[u] = tot++;
}
int low[maxn], dfn[maxn], num, in[maxn], scc[maxn];
int Stack[maxn], top, cnt;
void tarjan(int u) {
   low[u] = dfn[u] = ++num;
   Stack[top++] = u;
   in[u] = 1;
   for(int i = head[u]; i != -1; i = edge[i].nxt) {
       int v = edge[i].v;
       if(!dfn[v]) {
           tarjan(v);
           low[u] = min(low[u], low[v]);
      } else if(in[v]) {
           low[u] = min(low[u], dfn[v]);
      }
  }
   if(low[u] == dfn[u]) {
       int v;
       cnt++;
       do {
           v = Stack[--top];
           in[v] = 0;
           scc[v] = cnt;
      } while(u != v);
  }
}
bool Two_sat() {
   for(int i = 0; i < 2 * n; i++) {
       if(!dfn[i]) {
           tarjan(i);
      }
  }
   for(int i = 0; i < n; i++) {
       if(scc[i * 2] == scc[i * 2 + 1]) return false;
  }
   return true;
}
int main() {
#ifndef ONLINE_JUDGE
   FIN
#endif
   while(~scanf("%d%d", &n, &m)) {
       memset(head, -1, sizeof(head));
       memset(dfn, 0, sizeof(dfn));
       memset(low, 0, sizeof(low));
       memset(scc, 0, sizeof(scc));
       tot = 0;
       num = 0;
       top = 0;
       cnt = 0;
       for(int i = 1; i <= m; i++) {
           int a, vala;
           int b, valb;
           scanf("%d%d%d%d", &a, &b,  &vala, &valb);
           a = a * 2 + vala;
           b = b * 2 + valb;
           add_edge(a, b ^ 1);  //选A必选B'
           add_edge(b, a ^ 1);  //选B必选A'
      }
       if(Two_sat()) {
           printf("YES\n");

      } else {
           printf("NO\n");
      }
  }
   return 0;
}

洛谷P4782

传送门:https://www.luogu.org/problem/P4782

题意:

2-sat模板题

题解:

建边

为或为​

建图

代码:

/**
*        ┏┓    ┏┓
*        ┏┛┗━━━━━━━┛┗━━━┓
*        ┃       ┃  
*        ┃   ━    ┃
*        ┃ >   < ┃
*        ┃       ┃
*        ┃... ⌒ ...  ┃
*        ┃       ┃
*        ┗━┓   ┏━┛
*          ┃   ┃ Code is far away from bug with the animal protecting          
*          ┃   ┃   神兽保佑,代码无bug
*          ┃   ┃           
*          ┃   ┃       
*          ┃   ┃
*          ┃   ┃           
*          ┃   ┗━━━┓
*          ┃       ┣┓
*          ┃       ┏┛
*          ┗┓┓┏━┳┓┏┛
*           ┃┫┫ ┃┫┫
*           ┗┻┛ ┗┻┛
*/
// warm heart, wagging tail,and a smile just for you!
//
//                           _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                           O\ = /O
//                       ____/`---'\____
//                     .' \|     |// `.
//                     / \||| : |||// \
//                   / _||||| -:- |||||- \
//                   |   | \ - /// |   |
//                   | \_| ''\---/'' |   |
//                   \ .-\__ `-` ___/-. /
//                 ___`. .' /--.--\ `. . __
//               ."" '< `.___\_<|>_/___.' >'"".
//             | | : `- \`.;`\ _ /`;.`/ - ` : | |
//             \ \ `-.   \_ __\ /__ _/   .-` / /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                           `=---='
//       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//                     佛祖保佑     永无BUG
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 2e6 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double Pi = acos(-1);
LL gcd(LL a, LL b) {
   return b ? gcd(b, a % b) : a;
}
LL lcm(LL a, LL b) {
   return a / gcd(a, b) * b;
}
double dpow(double a, LL b) {
   double ans = 1.0;
   while(b) {
       if(b % 2)ans = ans * a;
       a = a * a;
       b /= 2;
  } return ans;
}
LL quick_pow(LL x, LL y) {
   LL ans = 1;
   while(y) {
       if(y & 1) {
           ans = ans * x % mod;
      } x = x * x % mod;
       y >>= 1;
  } return ans;
}
struct EDGE {
   int v, nxt;
} edge[maxn << 1];
   int n, m;
int head[maxn], tot;
void add_edge(int u, int v) {
   edge[tot].v = v;
   edge[tot].nxt = head[u];
   head[u] = tot++;
}
int low[maxn], dfn[maxn], num,in[maxn],scc[maxn];
int Stack[maxn],top, cnt;
void tarjan(int u) {
   low[u] = dfn[u] = ++num;
   Stack[top++] = u;
   in[u] = 1;
   for(int i = head[u]; i != -1; i = edge[i].nxt) {
       int v = edge[i].v;
       if(!dfn[v]) {
           tarjan(v);
           low[u] = min(low[u], low[v]);
      } else if(in[v]) {
           low[u] = min(low[u], dfn[v]);
      }
  }
   if(low[u] == dfn[u]) {
       int v;
       cnt++;
       do {
           v = Stack[--top];
           in[v] = 0;
           scc[v] = cnt;
      } while(u != v);
  }
}
bool Two_sat() {
   for(int i = 1; i <= 2 * n; i++) {
       if(!dfn[i]) {
           tarjan(i);
      }
  }
   for(int i = 1; i <= n; i++) {
       if(scc[i] == scc[i + n]) return false;
  }
   return true;
}
int main() {
#ifndef ONLINE_JUDGE
   FIN
#endif
   memset(head, -1, sizeof(head));
   tot = 0;

   scanf("%d%d", &n, &m);
   for(int i = 1; i <= m; i++) {
       int a, vala;
       int b, valb;
       scanf("%d%d%d%d", &a, &vala, &b, &valb);
       int nota = !vala;
       int notb = !valb;
       add_edge(a + nota * n, b + valb * n);
       add_edge(b + notb * n, a + vala * n);
  }
   if(Two_sat()) {
       printf("POSSIBLE\n");
       for(int i = 1; i <= n; i++) {
           printf("%d ", scc[i] > scc[i + n]);
      }
  } else {
       printf("IMPOSSIBLE\n");
  }
   return 0;
}

几个关于2-sat的题的更多相关文章

  1. Go Deeper HDU - 3715(2 - sat 水题 妈的 智障)

    Go Deeper Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  2. ocp 1Z0-043 131-205题解析

    131. Which three methods can you use to run an Automatic Database Diagnostic Monitor (ADDM) analysis ...

  3. poj3393[模拟题]

    Lucky and Good Months by Gregorian Calendar Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  4. 浙江大学PAT上机题解析之1014. 福尔摩斯的约会 (20)

    1014. 福尔摩斯的约会 (20) 时间限制   50 ms 内存限制   32000 kB 代码长度限制   8000 B 判题程序     Standard     作者     CHEN, Y ...

  5. java小题:福尔摩斯的约会

    原题地址:https://www.nowcoder.com/pat/6/problem/4040 防止广告嫌疑,原题为: 题目描述 大侦探福尔摩斯接到一张奇怪的字条:"我们约会吧! 3485 ...

  6. z3 巧解CTF逆向题

    z3 巧解逆向题 题目下载链接:http://reversing.kr/download.php?n=7 这次实验的题目为Reversing.kr网站中的一道题目. 题目要求: ReversingKr ...

  7. PAT 甲级真题题解(1-62)

    准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format  模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...

  8. Newtonsoft.Json C# Json序列化和反序列化工具的使用、类型方法大全 C# 算法题系列(二) 各位相加、整数反转、回文数、罗马数字转整数 C# 算法题系列(一) 两数之和、无重复字符的最长子串 DateTime Tips c#发送邮件,可发送多个附件 MVC图片上传详解

    Newtonsoft.Json C# Json序列化和反序列化工具的使用.类型方法大全   Newtonsoft.Json Newtonsoft.Json 是.Net平台操作Json的工具,他的介绍就 ...

  9. 标 题: Re: 总感觉IT没我大山东啥事?

    发信人: liuzhlai (liuzhlai), 信区: ITExpress 标  题: Re: 总感觉IT没我大山东啥事? 发信站: 水木社区 (Sat Aug 22 15:51:50 2015) ...

  10. java基础50道编程题

    50道JAVA基础编程练习题 [程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少? 程序分析 ...

随机推荐

  1. poj3294 后缀数组

    后缀数组多个字符串问题. 先求出height[]数组,然后二分求最大的长度. 但是条件需要改变.如果出现次数大于一般那就满足.然后就要解决如何判断那一段属于其中一个字符串. 所以先处理出长度.并且不断 ...

  2. ELK2之ELK的语法学习

    1.回顾 (1)es是什么? es是基于Apache Lucene的开源分布式(全文)搜索引擎,提供简单的RESTful API来隐藏Lucene的复杂性. es除了全文搜索引擎之外,还可以这样描述它 ...

  3. 基于颜色的R2V软件快速矢量化

    跟同学一起做SRTP,矢量化,作图的工作点名让我去做,人家说,谁让你是学地理信息的呢?哎,什么时候地理信息不再被别人当成制图画图的,我们专业就有希望了. 话虽然这么说,但工作还是要去做. (进入正题) ...

  4. WPF 线段Line过长渲染出现的问题

    原文:WPF 线段Line过长渲染出现的问题 在使用WPF的时候,可以做一个实验,在canvas里添加一条线段Line,StrokeThickness属性设置为1,然后通过放大canvas或者调整li ...

  5. ROS 设置串口USB软连接

    原创:未经同意,请勿转载 我们在windows 通过USB连接串口,在设备串口中可以观测到COM0或者COMx.当我们插入不同的USB口时会显示不同的COM. 在UBUNTU下,ROS下接收串口信息时 ...

  6. @NOIP2018 - D1T1@ 铺设道路

    目录 @题目描述@ @考场上的思路@ @比较正常的题解@ @题目描述@ 春春是一名道路工程师,负责铺设一条长度为 n 的道路. 铺设道路的主要工作是填平下陷的地表.整段道路可以看作是 n 块首尾相连的 ...

  7. SDUT-2132_数据结构实验之栈与队列二:一般算术表达式转换成后缀式

    数据结构实验之栈与队列二:一般算术表达式转换成后缀式 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 对于一个基于二元运 ...

  8. python 处理图像出现The lower bounary is neither an array of the same size and same type as src, nor a scalar in function inRange

    在用python处理图像过程中出现如下错误 导致这个错误的原因是im是二维,而lower_green和upper_green是三维,所以无法用inRange处理. 由上图可以看出image本来是具有高 ...

  9. saltStack 配置管理(也就是替换文件)

    目录  /srv/salt/base下面新建一个文件dns.sls /opt/resolv.conf_bak:     #这个是文件替换的位置,也就说替换到远程文件的/opt/resolv.conf_ ...

  10. Java中Map/List/Set .

    很实用,分享一下. 简单版本 复杂版本 参考: http://initbinder.com/articles/cheat-sheet-for-selecting-maplistset-in-java. ...