图论: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);
上一篇:unity, 非public变量需要加[SerializeField]才能序列化


下一篇:git bash 常用命令