您的位置:首页 > 其它

2-sat模版(转自acm再见)

2017-10-18 21:25 267 查看
//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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: