//2-sat模版
const int maxn=10005*3;
int n,m;
int a[maxn],b[maxn];
struct note
{
int to;
int nxt;
}edge[maxn*2];
int head[maxn];
int ip;
int dfn[maxn],low[maxn],sccno[maxn],cnt,scc,instack[maxn];
stack<int> stk;
void init()
{
memset(head,-1,sizeof(head));
ip=1;
}
void addedge(int u,int v)
{
edge[ip].to=v,edge[ip].nxt=head[u],head[u]=ip++;
}
// x = xval or y = yval
//void add_cluse(int x,int xval,int y,int yval)
//{
// x=x*2+xval;
// y=y*2+yval;
// addedge(x,y^1);
// addedge(y,x^1);
//}
void dfs(int u)
{
dfn[u]=low[u]=++scc;
stk.push(u);
instack[u]=1;
for (int i=head[u]; i!=-1; i=edge[i].nxt)
{
int v=edge[i].to;
if (!dfn[v])
{
dfs(v);
low[u]=min(low[u],low[v]);
}
else if (instack[v])
low[u]=min(low[u],dfn[v]);
}
if (low[u]==dfn[u])
{
cnt++;
int x;
do
{
x=stk.top();
stk.pop();
sccno[x]=cnt;
instack[x]=0;
}while (x!=u);
}
}
bool solve()
{
scc=cnt=0;
memset(sccno,0,sizeof(sccno));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(instack,0,sizeof(instack));
while (!stk.empty()) stk.pop();
for (int i=0; i<2*n; i++) if (!dfn[i]) dfs(i);
for (int i=0; i<2*n; i+=2)
{
if (sccno[i]==sccno[i^1]) return false;
}
return true;
}
2-sat模版(转自acm再见)
最新推荐文章于 2020-05-26 19:26:11 发布
本文介绍了一种解决2-可满足性问题(2-SAT)的算法实现,通过使用Tarjan算法进行强连通分量分析来判断是否存在冲突,确保命题逻辑公式能够找到至少一组解。代码中详细展示了如何构建图、进行深度优先搜索以及如何根据搜索结果判断是否存在解。
&spm=1001.2101.3001.5002&articleId=78278109&d=1&t=3&u=d0d1f74a1a62489e8a7a2557b5fc486e)
7230

被折叠的 条评论
为什么被折叠?



