BZOJ 2199 [Usaco2011 Jan]奶牛议会

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2199

题意:略。

思路:裸的2-SAT问题,但是因为要判断每个变量在所有解中是只能为真还是只能为假还是既可以为真又可以为假,所以模板要稍微修改一下,真假都要找一遍判断是否可行。

#pragma comment(linker, "/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define Key_Value ch[ch[root][1]][0]
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define clr(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=500000+5;
const int INF=0x3f3f3f3f;
const int P=1000000007;
const double PI=acos(-1.0);
template<typename T>
inline T read(T&x){
    x=0;int _f=0;char ch=getchar();
    while(ch<'0'||ch>'9')_f|=(ch=='-'),ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x=_f?-x:x;
}
struct TwoSAT{
    int n;
    vector<int>G[maxn*2];
    bool mark[maxn*2];
    int rate[maxn*2];
    int S[maxn*2],c;
 
    void init(int n){
        this->n=n;
        for (int i=0;i<n*2;i++) G[i].clear();
        memset(mark,false,sizeof(mark));
    }
 
    void add_clause(int x,int y){
        G[x].push_back(y);
    }
 
    bool dfs(int x){
        if (mark[x^1]) return false;
        if (mark[x]) return true;
        mark[x]=true;
        S[c++]=x;
        for (int i=0;i<(int)G[x].size();i++)
            if (!dfs(G[x][i])) return false;
        return true;
    }
 
    bool solve(){
        for (int i=0;i<n*2;i+=2){
            if (!mark[i] && !mark[i+1]){
                c=0;
                if (dfs(i)) rate[i]=1;
                while (c) mark[S[--c]]=false;
                c=0;
                if (dfs(i+1)) rate[i]+=2;
                while (c) mark[S[--c]]=false;
                if (!rate[i]) return false;
            }
        }
        return true;
    }
}A;
int n,m,a,b;
char s1[2],s2[2];
int main(){
    while (~scanf("%d%d",&n,&m)){
        A.init(n);
        for (int i=1;i<=m;i++){
            scanf("%d%s%d%s",&a,s1,&b,s2);
            a--,b--;
            a*=2,b*=2;
            if (s1[0]=='Y' && s2[0]=='Y'){
                A.add_clause(a,b+1);
                A.add_clause(b,a+1);
            }
            else if (s1[0]=='Y' && s2[0]=='N'){
                A.add_clause(a,b);
                A.add_clause(b+1,a+1);
            }
            else if (s1[0]=='N' && s2[0]=='Y'){
                A.add_clause(a+1,b+1);
                A.add_clause(b,a);
            }
            else if (s1[0]=='N' && s2[0]=='N'){
                A.add_clause(a+1,b);
                A.add_clause(b+1,a);
            }
        }
        if (!A.solve()) puts("IMPOSSIBLE");
        else{
            for (int i=0;i<n*2;i+=2){
                if (A.rate[i]==3) putchar('?');
                else if (A.rate[i]==1) putchar('N');
                else putchar('Y');
            }
            puts("");
        }
    }
    return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注