zoukankan      html  css  js  c++  java
  • POJ 3678 Katu Puzzle (2-SAT)

                                                                         Katu Puzzle
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 11429   Accepted: 4233

    Description

    Katu Puzzle is presented as a directed graph G(VE) with each edge e(a, b) labeled by a boolean operator op (one of AND, OR, XOR) and an integer c (0 ≤ c ≤ 1). One Katu is solvable if one can find each vertex Vi a value Xi (0 ≤ X≤ 1) such that for each edge e(a, b) labeled by op and c, the following formula holds:

     Xa op Xb = c

    The calculating rules are:

    AND 0 1
    0 0 0
    1 0 1
    OR 0 1
    0 0 1
    1 1 1
    XOR 0 1
    0 0 1
    1 1 0

    Given a Katu Puzzle, your task is to determine whether it is solvable.

    Input

    The first line contains two integers N (1 ≤ N ≤ 1000) and M,(0 ≤ M ≤ 1,000,000) indicating the number of vertices and edges.
    The following M lines contain three integers (0 ≤ a < N), b(0 ≤ b < N), c and an operator op each, describing the edges.

    Output

    Output a line containing "YES" or "NO".

    Sample Input

    4 4
    0 1 1 AND
    1 2 1 OR
    3 2 0 AND
    3 0 0 XOR

    Sample Output

    YES

    Hint

    X0 = 1, X1 = 1, X2 = 0, X3 = 1.
     
    建图有问题,其他的很简单,容我仔细思考一下。
     
    #include<iostream>
    #include<vector>
    #include<stack>
    #include<cstdio>
    using namespace std;
    int n,m;
    vector<int>u[200024];
    stack<int>st;
    int dfn[200024],sig,low[200024],color[20024],index;
    bool book[200024];
    void init()
    {
        scanf("%d%d",&n,&m);
        char s[10];
        int x,y,c;
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&x,&y,&c);
            scanf("%s",s);
            if(s[0]=='A'){
                if(c==1){
                    u[y].push_back(y+n);//不理解
                    u[x].push_back(x+n);//不理解
                }
                if(c==0){
                    u[x+n].push_back(y);
                    u[y+n].push_back(x);
                }
            }
            else if(s[0]=='X'){
                if(c==1){
                    u[x+n].push_back(y);
                    u[x].push_back(y+n);
                    u[y+n].push_back(x);
                    u[y].push_back(x+n);
                }
                if(c==0){
                    u[x+n].push_back(y+n);
                    u[x].push_back(y);
                    u[y+n].push_back(x+n);
                    u[y].push_back(x);
                }
            }
            else if(s[0]=='O'){
                if(c==1){
                    u[x].push_back(y+n);
                    u[y].push_back(x+n);
                }
                if(c==0){
                    u[y+n].push_back(y);//不理解
                    u[x+n].push_back(x);//不理解
                }
            }
    
        }
    }
    
    void tarjan(int t)
    {
        dfn[t]=low[t]=++index;
        book[t]=true;
        st.push(t);
        int siz=u[t].size();
        for(int i=0;i<siz;i++){
            if(!dfn[u[t][i]]){
                tarjan(u[t][i]);
                low[t]=min(low[t],low[u[t][i]]);
            }
            if(book[u[t][i]]){
                low[t]=min(low[t],low[u[t][i]]);
            }
        }
        int miui;
        if(dfn[t]==low[t]){
            sig++;
            while(true){
                if(st.empty()){break;}
                miui=st.top();
                st.pop();
                book[miui]=false;
                color[miui]=sig;
                if(miui==t){break;}
            }
        }
    }
    
    
    bool solve()
    {
        for(int i=0;i<n;i++){
            if(!dfn[i]){
                tarjan(i);
            }
        }
    
        for(int i=0;i<n;i++){
            if(color[i]==color[i+n]&&color[i]!=0){
                return false;
            }
        }
        return true;
    }
    
    int main()
    {
        init();
        if(solve()){
            printf("YES
    ");
        }
        else printf("NO
    ");
    }
    

      

  • 相关阅读:
    css常用小知识点汇总(一)
    TweenMax的GSAP(GreenSock动画平台)GSAP,专业的Web动画库
    前端node面试题之---对比JS和NodeJS的区别
    Math.min() Math.max() Math.min().apply() Math.max() .apply()该如何使用???
    zrender的线性渐变
    webkit内核的浏览器常见7种分别是..
    node创建项目,要ejs模板引擎,不要jade怎么办?
    【正则】——深入正则表达式,手写常用正则表单验证
    【bug】—— ios scroll 滚动穿透
    【bug】—— H5页面在 ios 端滑动不流畅的问题
  • 原文地址:https://www.cnblogs.com/ZGQblogs/p/9402550.html
Copyright © 2011-2022 走看看