zoukankan      html  css  js  c++  java
  • POJ 3678 Katu Puzzle

    2-SAT简单题。位运算写的时候忘记加括号WA了一发....

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<queue>
    #include<vector>
    #include<stack>
    #include<algorithm>
    using namespace std;
    
    const int maxn=2000+10;
    int N,M;
    char s[1000];
    int a,b,c;
    int a1=0,a2=1,b1=0,b2=1;
    
    stack<int>S;
    vector<int>G[maxn];
    vector<int>FG[maxn];
    int Belong[maxn];
    int flag[maxn];
    int Block;
    
    void init()
    {
        for(int i=0; i<maxn; i++) G[i].clear();
        for(int i=0; i<maxn; i++) FG[i].clear();
        memset(Belong,0,sizeof Belong);
        memset(flag,0,sizeof flag);
        while(!S.empty()) S.pop();
        Block=0;
    }
    
    void addEgde(int x,int y)
    {
        G[x].push_back(y);
        FG[y].push_back(x);
    }
    
    void dfs1(int now)
    {
        flag[now]=1;
        for(int i=0; i<G[now].size(); i++)
            if(!flag[G[now][i]])
                dfs1(G[now][i]);
        S.push(now);
    }
    
    void dfs2(int now)
    {
        Belong[now]=Block;
        for(int i=0; i<FG[now].size(); i++)
            if(!Belong[FG[now][i]])
                dfs2(FG[now][i]);
    }
    
    bool judge()
    {
        for(int i=0; i<2*N; i++) if(!flag[i]) dfs1(i);
        while(!S.empty())
        {
            int Top=S.top();
            S.pop();
            if(!Belong[Top])
            {
                Block++;
                dfs2(Top);
            }
        }
        for(int i=0; i<N; i++)
            if(Belong[2*i]==Belong[2*i+1])
                return 0;
        return 1;
    }
    
    int main()
    {
        while(~scanf("%d%d",&N,&M))
        {
            init();
            for(int i=0; i<M; i++)
            {
                scanf("%d%d%d%s",&a,&b,&c,s);
                if(strcmp("AND",s)==0)
                {
                    if((a1&b1)!=c)
                    {
                        addEgde(2*a,2*b+1);
                        addEgde(2*b,2*a+1);
                    }
    
                    if((a1&b2)!=c)
                    {
                        addEgde(2*a,2*b);
                        addEgde(2*b+1,2*a+1);
                    }
    
                    if((a2&b1)!=c)
                    {
                        addEgde(2*a+1,2*b+1);
                        addEgde(2*b,2*a);
                    }
    
                    if((a2&b2)!=c)
                    {
                        addEgde(2*a+1,2*b);
                        addEgde(2*b+1,2*a);
                    }
    
                }
                else if(strcmp("OR",s)==0)
                {
                    if((a1|b1)!=c)
                    {
                        addEgde(2*a,2*b+1);
                        addEgde(2*b,2*a+1);
                    }
    
                    if((a1|b2)!=c)
                    {
                        addEgde(2*a,2*b);
                        addEgde(2*b+1,2*a+1);
                    }
    
                    if((a2|b1)!=c)
                    {
                        addEgde(2*a+1,2*b+1);
                        addEgde(2*b,2*a);
                    }
    
                    if((a2|b2)!=c)
                    {
                        addEgde(2*a+1,2*b);
                        addEgde(2*b+1,2*a);
                    }
                }
                else if(strcmp("XOR",s)==0)
                {
                    if((a1^b1)!=c)
                    {
                        addEgde(2*a,2*b+1);
                        addEgde(2*b,2*a+1);
                    }
    
                    if((a1^b2)!=c)
                    {
                        addEgde(2*a,2*b);
                        addEgde(2*b+1,2*a+1);
                    }
    
                    if((a2^b1)!=c)
                    {
                        addEgde(2*a+1,2*b+1);
                        addEgde(2*b,2*a);
                    }
    
                    if((a2^b2)!=c)
                    {
                        addEgde(2*a+1,2*b);
                        addEgde(2*b+1,2*a);
                    }
                }
            }
            if(judge()) printf("YES
    ");
            else printf("NO
    ");
        }
        return 0;
    }
  • 相关阅读:
    ini_set /ini_get函数功能-----PHP
    【转】那个什么都懂的家伙
    word 2007为不同页插入不同页眉页脚
    August 26th 2017 Week 34th Saturday
    【2017-11-08】Linux与openCV:opencv版本查看及库文件位置等
    August 25th 2017 Week 34th Friday
    August 24th 2017 Week 34th Thursday
    August 23rd 2017 Week 34th Wednesday
    August 22nd 2017 Week 34th Tuesday
    August 21st 2017 Week 34th Monday
  • 原文地址:https://www.cnblogs.com/zufezzt/p/4915590.html
Copyright © 2011-2022 走看看