zoukankan      html  css  js  c++  java
  • 题解 P4171 【[JSOI2010]满汉全席】

    题目链接:Link

    Problem

    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
    ",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;
    }
    
    
  • 相关阅读:
    Highcharts之饼图
    设计模式学习之原型模式
    jQuery学习之结构解析
    JS学习之闭包的理解
    JS学习之prototype属性
    JS学习之事件冒泡
    Mybatis学习之JDBC缺陷
    Spring学习之Aop的基本概念
    Struts学习之值栈的理解
    Struts学习之自定义结果集
  • 原文地址:https://www.cnblogs.com/happyZYM/p/11379579.html
Copyright © 2011-2022 走看看