zoukankan      html  css  js  c++  java
  • hdu1814 2-SAT 暴力搜

    Peaceful Commission

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2853    Accepted Submission(s): 901


    Problem Description
    The Public Peace Commission should be legislated in Parliament of The Democratic Republic of Byteland according to The Very Important Law. Unfortunately one of the obstacles is the fact that some deputies do not get on with some others. 

    The Commission has to fulfill the following conditions: 
    1.Each party has exactly one representative in the Commission, 
    2.If two deputies do not like each other, they cannot both belong to the Commission. 

    Each party has exactly two deputies in the Parliament. All of them are numbered from 1 to 2n. Deputies with numbers 2i-1 and 2i belong to the i-th party . 

    Task 
    Write a program, which: 
    1.reads from the text file SPO.IN the number of parties and the pairs of deputies that are not on friendly terms, 
    2.decides whether it is possible to establish the Commission, and if so, proposes the list of members, 
    3.writes the result in the text file SPO.OUT. 
     
    Input
    In the first line of the text file SPO.IN there are two non-negative integers n and m. They denote respectively: the number of parties, 1 <= n <= 8000, and the number of pairs of deputies, who do not like each other, 0 <= m <=2 0000. In each of the following m lines there is written one pair of integers a and b, 1 <= a < b <= 2n, separated by a single space. It means that the deputies a and b do not like each other. 
    There are multiple test cases. Process to end of file. 
     
    Output
    The text file SPO.OUT should contain one word NIE (means NO in Polish), if the setting up of the Commission is impossible. In case when setting up of the Commission is possible the file SPO.OUT should contain n integers from the interval from 1 to 2n, written in the ascending order, indicating numbers of deputies who can form the Commission. Each of these numbers should be written in a separate line. If the Commission can be formed in various ways, your program may write mininum number sequence. 
     
    Sample Input
    3 2 1 3 2 4
     
    Sample Output
    1 4 5
     

    第一道2-sat的题目,看了刘汝佳的算法竞赛p324,说的很详细了,比如(2*i-1) hate (2*j) ,选择(2*i-1)必然要选择(2*j-1),建立一条(2*i-1) -> (2*j-1)的边,同理,也可以得出(2*j)->(2*i),把这两条有向边称为对称边
    很容易得出,所有的关系都能推导出两条对称边
    还有一种情况就是,有些既可以选择2*k-1,也可以选择2*k,这时候,只要讨论2*k-1被选择时,能否得到结果,如果不能,必然要选择2*k
    深搜过程中,将所有节点染为白色,认为初始都没有被选择,当k被选择时,k被染为红色,(k^1)被染为蓝色,表示该节点在该次选择的过程中都不能选择了,如果出现深搜了蓝色节点,说明已经发生矛盾了,节点的选择有问题
    暴力深搜的复杂度为O(n*m)
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <cstdlib>
    #include <string>
    #include <stack>
    #include <vector>
    #include <algorithm>
    struct edge{
        int st;
        int to;
        int next;
    };
    const int inf = 0x3f3f3f;
    const int MAXN  = 8e3+10;
    const int MAXNN = 2e4+10;
    
    using namespace std;
    int first[2*MAXN];
    edge e[2*MAXNN];
    int color[2*MAXN];
    int S[2*MAXN];
    int top,l;
    int n,m;
    int flag;
    
    void init(){
        l = 0;
        memset(first,-1,sizeof(first));
        memset(color,0,sizeof(color));
        flag= 1;
    }
    
    void addEdge(int u,int v){
        e[l].st = u;
        e[l].to = v;
        e[l].next = first[u];
        first[u] = l++;
    }
    
    int dfs(int v){
        if(color[v]==2)return 0;    //如果深搜到已经染色为2的,不成立
        if(color[v]==1)return 1;
        color[v] = 1;    //本节点染色1
        color[v^1] = 2; //相邻的染色2
        S[top++] = v;
        for(int i=first[v];i!=-1;i=e[i].next){
            if(!dfs(e[i].to))return 0;
        }
        return 1;
    }
    
    int main()
    {
        int a,b;
        while(scanf("%d%d",&n,&m)!=EOF){
            init();
            for(int i=0;i<m;i++){
                scanf("%d%d",&a,&b);
                a--;
                b--;
                addEdge(a,b^1);
                addEdge(b,a^1);
            }
            int k;
            for(int i=0;i<2*n;i+=2){
                if(!color[i]){
                    top = 0;
                    if(!dfs(i)){
                        while(top){
                            k = S[--top];
                            color[k] = color[k^1] = 0;
                        }
                        if(!dfs(i+1)){  //2*i-1节点不能选择,必然要选择2*i节点,如果还不满足,说明no solution
                            flag = 0;
                            break;
                        }
                    }
                }
            }
    
            if(!flag){
            cout<<"NIE"<<endl;
            continue;
            }
            for(int i=0;i<2*n;i++){
                if(color[i]==1){
                    cout<<i+1<<endl;
                }
            }
        }
    
        //cout << "Hello world!" << endl;
        return 0;
    }
    /*
    6 6
    1 3
    4 6
    5 7
    9 11
    10 11
    5 12
    */
    View Code
     
     
    在一个谎言的国度,沉默就是英雄
  • 相关阅读:
    动态创建多个pictureBox控件并响应不同的事件
    浅谈程序效率问题
    winform 下实现消息传递机制
    兼容IE的最小最大高度CSS写法
    推荐14款web开源测试工具
    winform程序窗体相关设置
    Mysql配置及错误集合
    深入理解栈
    广而博,深而远
    POJ3009
  • 原文地址:https://www.cnblogs.com/EdsonLin/p/5468881.html
Copyright © 2011-2022 走看看