2-SAT(染色法,求字典序最小解)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Game_Acm/article/details/82429445
int tol,head[maxn];
struct edge
{
    int to,next;
}es[maxm];
void addedge( int u , int v )
{
    es[tol].to = v;
    es[tol].next = head[u];
    head[u] = tol++;
}
bool vis[maxn]; int S[maxn],top;
bool dfs( int u )
{
    if ( vis[u^1] ) return false;
    if ( vis[u] ) return true;
    vis[u] = true;
    S[top++] = u;
    for ( int i=head[u] ; i!=-1 ; i=es[i].next )
        if ( !dfs(es[i].to) ) return false;
    return true;
}
bool Twosat( int n )
{
    memset ( vis , false , sizeof(vis) );
    for ( int i=0 ; i<n ; i+=2 )
    {
        if ( vis[i]||vis[i^1] ) continue;
        top = 0;
        if ( !dfs(i) )
        {
            while ( top ) vis[S[--top]] = false;
            if ( !dfs(i^1) ) return false;
        }
    }
    return true;
}

猜你喜欢

转载自blog.csdn.net/Game_Acm/article/details/82429445
今日推荐