zoukankan      html  css  js  c++  java
  • hdu1814(2-SAT)

    2-SAT 求出可能的解,但是这个解要是字典序最小的,所以只能采用2-SAT基本思想来解。

    从小到大开始,对一个可能的点染色,染为1,然后dfs其所有能到达的点,如果其中出现一个已经标号为-1的话,那么就说明这个点不能选。  然后把之前标好的号清除掉。 如果没有出现的话,那么就保持染色。 如果出现同一集合的两个点都不能染色的话就说明无解。 否则输出所有染为1的点。

    Peaceful Commission

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


    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
     
    Source
     
    Recommend
    威士忌
     
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <algorithm>
    #include <math.h>
    #include <map>
    #include <queue>
    #include <sstream>
    #include <iostream>
    using namespace std;
    #define INF 0x3fffffff
    #define N 21000
    
    struct node
    {
        int to,next;
    }edge[50050];
    
    int n,m;
    int pre[N],cnt;
    int mark[N];
    int stk[N]; //用来存一次染色的点
    int top;
    
    int dfs(int s)
    {
        mark[s]=1;
        mark[s^1]=-1;
        stk[top++]=s;
    
        for(int p=pre[s];p!=-1;p=edge[p].next)
        {
            int v=edge[p].to;
            if(mark[v]==0)
            {
                if( dfs(v)==0 ) return 0;
                /*
                stk[top++]=v;
                mark[v]=1; //那么这个点就可以选
                mark[v^1]=-1;  //这个点就不可以选 ,可以知道的时候每次标号都是标两个,所以不会重复
                */
            }
            else
            if(mark[v]==-1)//说明不行。 那么直接放回! 不需要染色了
            {
                return 0;
            }
        }
        return 1;
    }
    
    void add_edge(int u,int v)
    {
        edge[cnt].to=v;
        edge[cnt].next=pre[u];
        pre[u]=cnt++;
    }
    
    
    int main()
    {
        //freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        //freopen("C:\Users\Administrator\Desktop\in.txt","w",stdout);
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            cnt=0;
            memset(pre,-1,sizeof(pre));
            for(int i=0;i<m;i++)
            {
                int x,y;
                scanf("%d%d",&x,&y);
                x--; y--;
                add_edge(x,y^1);
                add_edge(y,x^1);
            }
    
            memset(mark,0,sizeof(mark)); //当mark==0 的时候用
            int flag=0;
            for(int i=0;i<2*n;i+=2)
            {
                top=0;
                if(mark[i]==1)
                {
                    continue;
                }
                if( (mark[i]!=-1 )&& dfs(i)==1 )//表示可以选择这个点,并且已经染好色了
                {
                    continue;
                }
                else
                {
                    // 如果之前的那个点不能选的话. 那把stk中的东西清掉
                    for(int j=0;j<top;j++)
                    {
                        mark[stk[j]]=0;
                        mark[stk[j]^1]=0;
                    }
                    top=0;
    
                    if(mark[i^1]==1) continue; //把这个点标成1 后,肯定已经把需要标记的选好
    
                    if(mark[i^1]==-1 || dfs(i^1)==0) //如果这个点也不能选,那么就是无解了
                    {
                        flag=1;
                        break;
                    }
    
                }
            }
            if(flag)
                printf("NIE
    ");
            else
            {
                for(int i=0;i<2*n;i++)
                {
                    if(mark[i]==1)
                        printf("%d
    ",i+1);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    C#---将数据库数据转换为json格式
    ASP.NET ---根据值让树中某一节点选中
    SQL---查询树中某个节点及其所有子节点
    CSS---相对定位笔记
    CSS---绝对定位笔记
    滑雪
    Self Numbers
    Lotto
    Parencodings
    Robot Motion
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/3348507.html
Copyright © 2011-2022 走看看