zoukankan      html  css  js  c++  java
  • UVA 10319 Manhattan 2-sat

    不难想到是2-sat,这题最繁的就是加边的了

    //#pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<iostream>
    #include<sstream>
    #include<cmath>
    #include<climits>
    #include<string>
    #include<map>
    #include<queue>
    #include<vector>
    #include<stack>
    #include<set>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<int,int> pii;
    #define pb(a) push(a)
    #define INF 0x1f1f1f1f
    #define lson idx<<1,l,mid
    #define rson idx<<1|1,mid+1,r
    #define PI  3.1415926535898
    template<class T> T min(const T& a,const T& b,const T& c) {
        return min(min(a,b),min(a,c));
    }
    template<class T> T max(const T& a,const T& b,const T& c) {
        return max(max(a,b),max(a,c));
    }
    void debug() {
    #ifdef ONLINE_JUDGE
    #else
    
        freopen("d:\in1.txt","r",stdin);
        freopen("d:\out1.txt","w",stdout);
    #endif
    }
    int getch() {
        int ch;
        while((ch=getchar())!=EOF) {
            if(ch!=' '&&ch!='
    ')return ch;
        }
        return EOF;
    }
    
    const int maxn=100;
    struct twosat
    {
        int n;
        int mark[maxn*2];
        vector<int> g[maxn*2];
        int s[maxn],c;
        void init(int n)
        {
            this->n=n;
            memset(mark,0,sizeof(mark));
            for(int i=0;i<2*n;i++)
                g[i].clear();
        }
        bool dfs(int u)
        {
            if(mark[u^1])return false;
            if(mark[u])return true;
            mark[u]=1;
            s[c++]=u;
            for(int i=0;i<g[u].size();i++)
            {
                if(!dfs(g[u][i]))return false;
            }
            return true;
        }
        bool solve()
        {
            for(int i=0;i<2*n;i+=2)
            {
                if(!mark[i]&&!mark[i+1])
                {
                    c=0;
                    if(!dfs(i))
                    {
                        while(c)mark[s[--c]]=0;
                        if(!dfs(i+1))return false;
                    }
                }
            }
            return true;
        }
        void add_caluse(int x,int xval,int y,int yval)
        {
            x=x*2+xval;
            y=y*2+yval;
            g[x].push_back(y);
           // printf("%d->>%d
    ",x,y);
        }
    };
    
    twosat solver;
    int dir(int x1,int y1,int x2,int y2)
    {
        int dx=x2-x1;
        int dy=y2-y1;
        if(dx<0||dy<0)return 0;
        else return 1;
    }
    void add(int n)
    {
        int a,b,x,y;
        scanf("%d%d%d%d",&a,&b,&x,&y);
        a--;b--;x--;y--;
        if(x!=a)
        {
            if(b!=y)
                solver.add_caluse(b+n,dir(x,b,a,b),a,dir(a,b,a,y));
            solver.add_caluse(b+n,dir(x,b,a,b),y+n,dir(a,y,x,y));
            if(b!=y)
                solver.add_caluse(y+n,dir(x,y,a,y),x,dir(x,b,x,y));
            solver.add_caluse(y+n,dir(x,y,a,y),b+n,dir(a,b,x,b));
    
        }
        if(b!=y)
        {
            if(a!=x)
                solver.add_caluse(a,dir(a,y,a,b),b+n,dir(a,b,x,b));
            solver.add_caluse(a,dir(a,y,a,b),x,dir(x,b,x,y));
            if(a!=x)
                solver.add_caluse(x,dir(x,y,x,b),y+n,dir(a,y,x,y));
            solver.add_caluse(x,dir(x,y,x,b),a,dir(a,b,a,y));
        }
    
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        int t;
        scanf("%d",&t);
        for(int ca=1;ca<=t;ca++)
        {
            int n,m,k;
            scanf("%d%d%d",&n,&m,&k);
            solver.init(n+m);
            for(int i=1;i<=k;i++)
                add(n);
            printf("%s
    ",solver.solve()?"Yes":"No");
        }
        return 0;
    }
    View Code
  • 相关阅读:
    【转】Windows7 下安装 JDK 7 时版本冲突问题解决
    【转】Android开发之旅:环境搭建及HelloWorld
    android开发板
    win7重装系统的配置步骤
    caffe 源码阅读
    caffe 源码阅读
    Python 图像处理: 生成二维高斯分布蒙版
    学习 protobuf(一)—— ubuntu 下 protobuf 2.6.1 的安装
    学习 protobuf(一)—— ubuntu 下 protobuf 2.6.1 的安装
    CMake 添加头文件目录,链接动态、静态库(添加子文件夹)
  • 原文地址:https://www.cnblogs.com/BMan/p/3620760.html
Copyright © 2011-2022 走看看