2-SAT入门
大概学了一下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入门的更多相关文章
- Quartz 入门详解
Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Quartz可以用来创建简单或为运行十个,百个, ...
- Yii2框架RESTful API教程(一) - 快速入门
前不久做一个项目,是用Yii2框架写一套RESTful风格的API,就去查了下<Yii 2.0 权威指南 >,发现上面写得比较简略.所以就在这里写一篇教程贴,希望帮助刚接触Yii2框架RE ...
- Quartz应用实践入门案例二(基于java工程)
在web应用程序中添加定时任务,Quartz的简单介绍可以参看博文<Quartz应用实践入门案例一(基于Web应用)> .其实一旦学会了如何应用开源框架就应该很容易将这中框架应用与自己的任 ...
- angular入门
angular入门 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=&q ...
- 161103、Spring Boot 入门
Spring Boot 入门 spring Boot是Spring社区较新的一个项目.该项目的目的是帮助开发者更容易的创建基于Spring的应用程序和服务,让更多人的人更快的对Spring进行入门体验 ...
- Spring入门学习(一)
SpringMVC基础平台补充(2016.03.03) 如果想要开发SpringMVC,那么前期依次安装好:JDK(jdk-8u74-windows-x64,安装后配置环境变量JAVA_HOME和CL ...
- Redis入门教程:特性及数据类型的操作
虽然Redis已经很火了,相信还是有很多同学对Redis只是有所听闻或者了解并不全面,下面是一个比较系统的Redis介绍,对Redis的特性及各种数据类型及操作进行了介绍.是一个很不错的Redis入门 ...
- 史上最全!信息安全入门指南<转>
以下所列出的链接均为在线文档,有志于信息安全的爱好者可由此作为入门指南. 背景知识 常规知识 Sun认证-Solaris 9&10安全管理员学习指南 PicoCTF资料 应用软件安全 OWAS ...
- Python自动化运维之1、Python入门
Python简介 python是吉多·范罗苏姆发明的一种面向对象的脚本语言,可能有些人不知道面向对象和脚本具体是什么意思,但是对于一个初学者来说,现在并不需要明白.大家都知道,当下全栈工程师的概念很火 ...
- python数据分析入门学习笔记
学习利用python进行数据分析的笔记&下星期二内部交流会要讲的内容,一并分享给大家.博主粗心大意,有什么不对的地方欢迎指正~还有许多尚待完善的地方,待我一边学习一边完善~ 前言:各种和数据分 ...
随机推荐
- html div内第二行文字显示不下的时候才用省略号代替 css实现
有时候文字太多,但为了美观想要在第二行的时候才显示省略号,而不是第一行超出马上就出现省略号 下面是css代码: overflow:hidden;text-overflow: ellipsis;//显示 ...
- 使用advanced_installer将.net web程序打包为安装程序
当项目开发完成之后,需要给客户使用时,总不能将发布后的文件全部放一起压缩后直接给客户吧,然后客户需要自行搭建环境修改配置等等,体验太差了,这时候我们就需要使用一种打包工具了,查了一些资料之后,我选择使 ...
- Objective-C 封装 继承 多态
封装 #import <Foundation/Foundation.h> @interface Person : NSObject { //@public int _age; } - (v ...
- linux系统简单命令
# uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # hostn ...
- JavaScript 的一些基础知识
JavaScript基本语法 调试 打开 Chrome 开发工具 Win F12 Mac Command + Option + I 输入代码.测试执行 var str = 'evenyao' cons ...
- day-20 tensorflow持久化之入门学习
如果不对模型参数进行保存,当训练结束以后,模型也在内存中被释放,下一轮又需要对模型进行重新训练,有没有一种方法,可以利用之前已经训练好的模型参数值,直接进行模型推理或者继续训练?这里需要引入一个数据之 ...
- aishell数据处理为thchs30格式
目录 aishell数据转换格式 aishell数据转化方法 aishell数据格式对于用神经网络处理数据的同学来说比较不友善,因为他只有文字转录和音素级别的转录,并没有拼音的转录. 而thchs30 ...
- HADOOP docker(十):hdfs 结构体系
1.简介2.namenode和datanode3.The File System Namespace 文件系统命名空间4.Data Replication 数据复制5.Replica Placemen ...
- HDU 3062 Party(2-SAT模版题)
Problem Description 有n对夫妻被邀请参加一个聚会,因为场地的问题,每对夫妻中只有1人可以列席.在2n 个人中,某些人之间有着很大的矛盾(当然夫妻之间是没有矛盾的),有矛盾的2个人是 ...
- 静态类型&动态类型
何时使用:使用存在继承关系的类型时,必须将一个变量或其他表达式的静态类型与该表达式表示对象的动态类型区分开来 静态类型:表达式的静态类型在编译时总是已知的,它是变量声明时的类型或表达式生成的类型 动态 ...