• UVA 11294 Wedding 2sat


    训练指南上的翻译错了,只有一对新娘新郎,其他是夫妻。

    //#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=66;
    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();
        }
        void add_clause(int x,int xval,int y,int yval)
        {
            x=x*2+xval;
            y=y*2+yval;
            g[x].push_back(y^1);
            g[y].push_back(x^1);
        }
        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;
        }
    }solver;
    
    int main()
    {
        int n,m;
        while(scanf("%d%d",&n,&m)!=EOF&&(n||m))
        {
            solver.init(2*n);
            for(int i=0;i<n;i++)
            {
                solver.add_clause(i*2,0,i*2+1,0);
                solver.add_clause(i*2,1,i*2+1,1);
            }
            int flag=1;
            for(int i=0;i<m;i++)
            {
                char a[5],b[5];
                scanf("%s%s",a,b);
                int j=0,k=0;
                while(a[j]<='9'&&a[j]>='0')j++;
                while(b[k]<='9'&&b[k]>='0')k++;
                int x=0,y=0;
                for(int d=0;d<j;d++)
                    x=x*10+a[d]-'0';
                for(int d=0;d<k;d++)
                    y=y*10+b[d]-'0';
    
                x=x*2+(a[j]=='w'?1:0);
                y=y*2+(b[k]=='w'?1:0);
                //solver.add_clause(x,0,y,0);
                solver.add_clause(x,1,y,1);
            }
    
            solver.mark[1]=1;
            solver.mark[2]=1;
            flag=solver.solve();
            if(!flag)
                printf("bad luck
    ");
            else
            {
                for(int i=1;i<n;i++)
                {
                    printf("%d%c%c",i,solver.mark[i*4]?'h':'w',i==n-1?'
    ':' ');
                }
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    734. Sentence Similarity
    Rainbow Sort
    692. Top K Frequent Words
    347. Top K Frequent Elements
    12. Integer to Roman
    13. Roman to Integer
    109. Convert Sorted List to Binary Search Tree
    用表单写兴趣爱好的程序
    方法重载与方法重写的区别
    super和this的区别
  • 原文地址:https://www.cnblogs.com/BMan/p/3619313.html
Copyright © 2020-2023  润新知