图论:2-SAT模板
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std; const int maxn = _____; struct TwoSAT
{
int n;
vector<int> G[maxn*];
bool mark[maxn*];
int S[maxn*], c; bool dfs(int x)
{
if (mark[x^]) return false;
if (mark[x]) return true;
mark[x] = true;
S[c++] = x;
for (int i = ; i < G[x].size(); i++)
if (!dfs(G[x][i])) return false;
return true;
} void init(int n) // 一定要注意初始化的点数,别弄错
{
this->n = n;
for (int i = ; i < n*; i++) G[i].clear();
memset(mark, , sizeof(mark));
} // x = xval or y = yval
void add_clause(int x, int xval, int y, int yval) // 编号从0~n-1
{
x = x * + xval;
y = y * + yval;
G[x^].push_back(y);
G[y^].push_back(x);
} bool solve()
{
for(int i = ; i < n*; i += )
if(!mark[i] && !mark[i+])
{
c = ;
if(!dfs(i))
{
while(c > ) mark[S[--c]] = false;
if(!dfs(i+)) return false;
}
}
return true;
}
}; TwoSAT solver;
////////////////////////////////////////////////////
//二分搜索,2-SAT中经常会用到
int L=____,R=____;
while(L < R)
{
int M = L + (R-L+)/;
if(test(M)) L = M; // M满足条件的话test返回真
else R = M-;
}
printf("%d\n",L);
图论:2-SAT模板的更多相关文章
- 图论算法-Tarjan模板 【缩点;割顶;双连通分量】
图论算法-Tarjan模板 [缩点:割顶:双连通分量] 为小伙伴们总结的Tarjan三大算法 Tarjan缩点(求强连通分量) int n; int low[100010],dfn[100010]; ...
- 2 - sat 模板(自用)
2-sat一个变量两种状态符合条件的状态建边找强连通,两两成立1 - n 为第一状态(n + 1) - (n + n) 为第二状态 例题模板 链接一 POJ 3207 Ikki's Story IV ...
- ACM - 图论- 网络流 - 算法模板
\(EK\) 算法模板 #include <iostream> #include <queue> #include<string.h> using namespac ...
- 图论--割边--Tarjan模板
#include<iostream> #include<stdio.h> #include<vector> using namespace std; const i ...
- 图论--割点--Tarjan模板
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> ...
- 图论--最小生成树--Kruscal 模板
#include<iostream> #include<queue> #include<algorithm> #include<set> #includ ...
- ACM模板合集
写在前面: 第一年小白拿铜牌,第二年队友出走,加上疫情原因不能回校训练导致心底防线彻底崩盘,于是选择退役. 自从退役之后,一直想我打了那么久的ACM,什么也没留下觉得很难受,突然想到我打ACM的时候, ...
- poj1236Network of Schools Tarjan裸题
其实就是手打了个Tarjan的模板 输出的时候注意是入度为0的点的个数和max(入度0的个数,出度0的个数),在n=1时特判为0即可 ——以后图论要渐渐模板化,方便使用 #include <cs ...
- 洛谷 P2194 HXY烧情侣【Tarjan缩点】 分析+题解代码
洛谷 P2194 HXY烧情侣[Tarjan缩点] 分析+题解代码 题目描述: 众所周知,HXY已经加入了FFF团.现在她要开始喜(sang)闻(xin)乐(bing)见(kuang)地烧情侣了.这里 ...
随机推荐
- Android笔记(二):从savedIndstanceState发散
savedIndstanceState savedIndstanceState位于ActivityonCreate(Bundle savedInstanceState)方法的参数中.对这个参数的理解要 ...
- (转)iOS 各种控件默认高度(图示)
1.状态栏 状态栏一般高度为20像素,在打手机或者显示消息时会放大到40像素高,注意,两倍高度的状态栏在好像只能在纵向的模式下使用.如下图 用户可以隐藏状态栏,也可以将状态栏设置为灰色,黑色或者半透明 ...
- 你需要知道的九大排序算法【Python实现】之基数排序
八.基数排序 基本思想:基数排序(radix sort)属于"分配式排序"(distribution sort),又称"桶子法"(bucket sort)或bi ...
- Horizontal,vertical,Input_Mouse,Input_Key
鼠标获取 using UnityEngine; using System.Collections; public class Input_Mouse : MonoBehaviour { void Up ...
- [ES6] WeakMap vs Map
WeakMap: is a type of Map where only objects can be passed as keys. Primitive data type -- such are ...
- [Javascript] Array methods in depth - some
some returns a boolean value after passing each item in the source array through the test function t ...
- Hibernate Validation各注解的用法
Bean Validation 中内置的 constraint @Null 被注释的元素必须为 null @NotNull 被注释的元素必须不为 null @AssertTrue 被注释的元素必须为 ...
- CSharp命名风格
1.大小写约定 为了区分一个标识符中的多个单词,把标识符中的每个单词的首字母大写.不要用下划线来区分单词,或者在标识符中任何地方使用下划线,有两种方式适合大写标识符的字母: PascalCasing( ...
- Ubunte 11.4 下安装 SSH遇到的问题
第一次安装报了一堆错,主要是也http 404 not found之类的,搜索了一番怀疑是apt的source list的问题. 网上找到一份替换之,我用的是搜狐的服务器.网址如下: http://b ...
- windows internal读书笔记
程序:指一个静态的指令序列,而进程则是一个容器,其中包含了当执行一个程序特定实例时所用到的各种资源.