大概学了一下2-SAT,写了一道模板和一道USACO

  输出一个方案的话,tarjan缩点后倒着拓扑,染色输出。

  求任何解下选哪个就得枚举每个点dfs来判断选哪个。  

HIT 1917(2-sat模板)

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#define ll long long
using namespace std;
const int maxn=,inf=1e9;
struct poi{int too,pre,x;}e[maxn*],e2[maxn*];
int last[maxn],last2[maxn],dfn[maxn],low[maxn],col[maxn],lack[maxn],ru[maxn],rs[maxn],st[maxn],op[maxn];
int n,m,x,y,z,tot,tot2,tott,top,color,flag;
void read(int &k)
{
int f=;k=;char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(c<=''&&c>='')k=k*+c-'',c=getchar();
k*=f;
}
void add(int x,int y){e[++tot].too=y;e[tot].x=x;e[tot].pre=last[x];last[x]=tot;}
void add2(int x,int y){e2[++tot2].too=y;e2[tot2].x=x;e2[tot2].pre=last2[x];last2[x]=tot2;}
int next(int x){return x&?x+:x-;}
void tarjan(int x)
{
dfn[x]=low[x]=++tott;st[++top]=x;lack[x]=top;
for(int i=last[x];i;i=e[i].pre)
if(!dfn[e[i].too])tarjan(e[i].too),low[x]=min(low[x],low[e[i].too]);
else if(!col[e[i].too])low[x]=min(low[x],dfn[e[i].too]);
if(dfn[x]==low[x])for(color++;top>=lack[x];top--)col[st[top]]=color;
}
void topsort()
{
top=;for(int i=;i<=color;i++)if(!ru[i])st[++top]=i;
while(top)
{
int now=st[top--];
if(!rs[now])rs[now]=,rs[op[now]]=;
for(int i=last2[now];i;i=e2[i].pre)
if(!(--ru[e2[i].too]))st[++top]=e2[i].too;
}
}
void clr()
{
color=top=tot=tot2=flag=;
memset(col,,sizeof(col));
memset(dfn,,sizeof(dfn));
memset(ru,,sizeof(ru));
memset(rs,,sizeof(rs));
memset(lack,,sizeof(lack));
memset(last,,sizeof(last));
memset(last2,,sizeof(last2));
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
clr();
for(int i=;i<=m;i++)read(x),read(y),add(x,next(y)),add(y,next(x));
for(int i=;i<=*n;i++)if(!dfn[i])tarjan(i);
for(int i=;i<=n;i++)if(col[i<<]==col[next(i<<)]){printf("NIE\n");flag=;break;}
else op[col[i<<]]=col[next(i<<)],op[col[next(i<<)]]=col[i<<];
if(flag)continue;
for(int i=;i<=tot;i++)if(col[e[i].x]!=col[e[i].too])add2(col[e[i].too],col[e[i].x]),ru[col[e[i].x]]++;
topsort();
for(int i=;i<=n;i++)if(rs[col[(i<<)]]==)printf("%d\n",i<<);else printf("%d\n",next(i<<));
}
return ;
}

bzoj 2199

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
using namespace std;
const int maxn=;
struct poi{int x,too,pre;}e[maxn],e2[maxn];
int n,m,t,tot,tot2,tott,top,color;
int dfn[maxn],low[maxn],col[maxn],last[maxn],last2[maxn],v[maxn],st[maxn],lack[maxn];
char ans[maxn];
void read(int &k)
{
int f=;k=;char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(c<=''&&c>='')k=k*+c-'',c=getchar();
k*=f;
}
int readd()
{
int x;read(x);char c=getchar();
while(c!='Y'&&c!='N')c=getchar();
if(c=='Y')return (x<<)^;
return x<<;
}
void add(int x,int y){e[++tot].too=y;e[tot].x=x;e[tot].pre=last[x];last[x]=tot;}
void add2(int x,int y){e2[++tot2].too=y;e2[tot2].x=x;e2[tot2].pre=last2[x];last2[x]=tot2;}
void dfs(int x)
{
v[x]=t;
for(int i=last2[x];i;i=e2[i].pre)
if(v[e2[i].too]!=t)dfs(e2[i].too);
}
bool check(int x)
{
t++;dfs(x);
for(int i=;i<=n;i++)
if(v[col[(i<<)^]]==t&&v[col[i<<]]==t)return ;
return ;
}
void tarjan(int x)
{
dfn[x]=low[x]=++tott;st[++top]=x;lack[x]=top;
for(int i=last[x];i;i=e[i].pre)
if(!dfn[e[i].too])tarjan(e[i].too),low[x]=min(low[x],low[e[i].too]);
else if(!col[e[i].too])low[x]=min(low[x],dfn[e[i].too]);
if(dfn[x]==low[x])for(color++;top>=lack[x];top--)col[st[top]]=color;
}
void dfss(int x)
{
v[x]=;
for(int i=last[x];i;i=e[i].pre)
if(!v[e[i].too])dfss(e[i].too);
}
int main()
{
read(n);read(m);
for(int i=;i<=m;i++)
{
int x=readd(),y=readd();
add(x^,y);add(y^,x);
}
for(int i=;i<=((n<<)^);i++)if(!dfn[i])tarjan(i);
for(int i=;i<=n;i++)if(col[(i<<)^]==col[i<<]){puts("IMPOSSIBLE");return ;}
for(int i=;i<=tot;i++)if(col[e[i].x]!=col[e[i].too])add2(col[e[i].x],col[e[i].too]);
for(int i=;i<=n;i++)
{
int x=check(col[(i<<)^]),y=check(col[i<<]);
if(!(x||y)){puts("IMPOSSIBLE");return ;}
else if(x&&y)ans[i]='?';
else if(x)ans[i]='Y';
else if(y)ans[i]='N';
}
for(int i=;i<=n;i++)putchar(ans[i]);
}

2-SAT入门的更多相关文章

  1. Quartz 入门详解

    Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Quartz可以用来创建简单或为运行十个,百个, ...

  2. Yii2框架RESTful API教程(一) - 快速入门

    前不久做一个项目,是用Yii2框架写一套RESTful风格的API,就去查了下<Yii 2.0 权威指南 >,发现上面写得比较简略.所以就在这里写一篇教程贴,希望帮助刚接触Yii2框架RE ...

  3. Quartz应用实践入门案例二(基于java工程)

    在web应用程序中添加定时任务,Quartz的简单介绍可以参看博文<Quartz应用实践入门案例一(基于Web应用)> .其实一旦学会了如何应用开源框架就应该很容易将这中框架应用与自己的任 ...

  4. angular入门

    angular入门 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=&q ...

  5. 161103、Spring Boot 入门

    Spring Boot 入门 spring Boot是Spring社区较新的一个项目.该项目的目的是帮助开发者更容易的创建基于Spring的应用程序和服务,让更多人的人更快的对Spring进行入门体验 ...

  6. Spring入门学习(一)

    SpringMVC基础平台补充(2016.03.03) 如果想要开发SpringMVC,那么前期依次安装好:JDK(jdk-8u74-windows-x64,安装后配置环境变量JAVA_HOME和CL ...

  7. Redis入门教程:特性及数据类型的操作

    虽然Redis已经很火了,相信还是有很多同学对Redis只是有所听闻或者了解并不全面,下面是一个比较系统的Redis介绍,对Redis的特性及各种数据类型及操作进行了介绍.是一个很不错的Redis入门 ...

  8. 史上最全!信息安全入门指南<转>

    以下所列出的链接均为在线文档,有志于信息安全的爱好者可由此作为入门指南. 背景知识 常规知识 Sun认证-Solaris 9&10安全管理员学习指南 PicoCTF资料 应用软件安全 OWAS ...

  9. Python自动化运维之1、Python入门

    Python简介 python是吉多·范罗苏姆发明的一种面向对象的脚本语言,可能有些人不知道面向对象和脚本具体是什么意思,但是对于一个初学者来说,现在并不需要明白.大家都知道,当下全栈工程师的概念很火 ...

  10. python数据分析入门学习笔记

    学习利用python进行数据分析的笔记&下星期二内部交流会要讲的内容,一并分享给大家.博主粗心大意,有什么不对的地方欢迎指正~还有许多尚待完善的地方,待我一边学习一边完善~ 前言:各种和数据分 ...

随机推荐

  1. hdu1175连连看(dfs+细节)

    连连看 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  2. Http的请求和响应

    请求有客户端发起:可分为4个部分,请求方法(Requestmethod).请求的网址(Request URL).请求头(Request Headers).请求体(Request Body) 1.请求方 ...

  3. (python)leetcode刷题笔记 01 TWO SUM

    1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...

  4. sql server存储特殊字符解决办法

    好久没来院子了,最近在学java了,再加上项目比较紧,最近都没怎么上,其实这几天在项目中学到不少东西,都能写下来,但是久而久之就忘了,还是得养成及时总结的好习惯啊,还有有时间一定要把那个小项目整理下来 ...

  5. POJ 3084 Panic Room(最大流最小割)

    Description You are the lead programmer for the Securitron 9042, the latest and greatest in home sec ...

  6. vue.js学习之 打包为生产环境后,页面为白色

    vue.js学习之 打包为生产环境后,页面为白色 一:配置问题 当我们将项目打包为生产环境后,在dist文件夹下打开index.html,会发现页面为白色. 1:打开config>index.j ...

  7. apache访问403错误

    1.排查selinux 2.目录权限 3.WEB主目录是否正确

  8. Python学习之路5 - 函数

    函数 定义方式: def func(): "这里面写函数的描述" 这里写代码 return x #如果没有返回值就叫"过程",函数和过程的区别就是有无返回值 实 ...

  9. eg_2

    2. 编写一个程序,输出在一个字符串中,指定的字符串出现的次数 第一种方法: public class Test { public static void main(String[] args) { ...

  10. yum 安装 redis php-redis

    yum 安装 redis php-redis   redis和php-redis在官方源上是没有的,需要安装其他的源,其他源的地址为 http://mirrors.ustc.edu.cn/fedora ...