题解 P4171 【[JSOI2010]满汉全席】

Problem

P4171.png

Solution

这道题目是一道很明显的2-SAT问题,但要注意变量是每个材料,值是蒙式或汉式,而不是“蒙式xx为1或汉式xx为1”。
目测没有必要用Tarjan,毕竟理论时间复杂度一样,常数需要时卡一卡就行了。。。

Code

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=205;
struct TwoSAT
{
    int n,stk[maxn*2],mark[maxn*2],c;
    vector<int> G[maxn*2];
    void init(int nn)
    {
        n=nn;
        for(int i=0;i<n*2;i++) G[i].clear();
        memset(mark,0,sizeof(mark));
    }
    void Add(int x,int xval,int y,int yval)
    {
//      printf("a[%d] equals to %d  or  a[%d] equals to %d\n",x,xval,y,yval);
        x=x*2+xval; y=y*2+yval;
        G[x^1].push_back(y);
        G[y^1].push_back(x);
    }
    bool dfs(int u)
    {
        if(mark[u^1]) return false;
        if(mark[u]) return true;
        mark[u]=1;
        stk[c++]=u;
        for(int v:G[u]) if(!dfs(v)) 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>0) mark[stk[--c]]=0;
                if(!dfs(i+1)) return false;
            }
        }
        return true;
    }
}alg;
int T,n,m,hsh[128];
int main()
{
#ifdef local
    freopen("pro.in","r",stdin);
#endif
    scanf("%d",&T);
    hsh['m']=0; hsh['h']=1;
    while(T-->0)
    {
        scanf("%d%d",&n,&m);
        alg.init(n);
        while(m-->0)
        {
            char a[10],b[10]; scanf("%s%s",a,b);
            int x,y; sscanf(a+1,"%d",&x); sscanf(b+1,"%d",&y);
            x--; y--;
            alg.Add(x,hsh[a[0]],y,hsh[b[0]]);
        }
        puts(alg.solve()?"GOOD":"BAD");
    }
    return 0;
}

转载于:https://www.cnblogs.com/happyZYM/p/11379579.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值