BZOJ 1823 [JSOI2010]满汉全席

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

题意:略。

思路:很难受,读入那里写挂了只认定数字为一位数然后一直WA...其实就是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=10000+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 S[maxn],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)){
                    while (c) mark[S[--c]]=false;
                    if (!dfs(i+1)) return false;
                }
            }
        }
        return true;
    }
}A;
int T,n,m,a,b;
char s1[maxn],s2[maxn];
int main(){
    for (read(T);T--;){
        scanf("%d%d",&n,&m);
        A.init(n);
        for (int i=1;i<=m;i++){
            scanf("%s%s",s1,s2);
            sscanf(s1+1,"%d",&a);
            sscanf(s2+1,"%d",&b);
            a--,b--;
            a<<=1,b<<=1;
            if (s1[0]=='m' && s2[0]=='m'){
                A.add_clause(a,b+1);
                A.add_clause(b,a+1);
            }
            else if (s1[0]=='m' && s2[0]=='h'){
                A.add_clause(a,b);
                A.add_clause(b+1,a+1);
            }
            else if (s1[0]=='h' && s2[0]=='m'){
                A.add_clause(a+1,b+1);
                A.add_clause(b,a);
            }
            else if (s1[0]=='h' && s2[0]=='h'){
                A.add_clause(a+1,b);
                A.add_clause(b+1,a);
            }
        }
        if (!A.solve()) puts("BAD");
        else puts("GOOD");
    }
    return 0;
}

发表评论

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