夏夜、

心若平似镜、何题不AC。

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!='\n')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\n",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\n",solver.solve()?"Yes":"No");
    }
    return 0;
}
View Code

 

 

posted on 2014-03-24 14:21  BMan、  阅读(164)  评论(0编辑  收藏  举报

导航