zoukankan      html  css  js  c++  java
  • HDU 1814 Peaceful Commission

    Peaceful Commission

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


    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*i-1 与 2*i 只能选一个 。

    然后m对数存在矛盾关系 。

    要求输出2-sat 可行解的最小序 , 如果不存在就输出NIE 。

    那么直接每次求到满足解时 ,存入ans数组 。

    一旦无法满足跳出输出NIE即可。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <stack>
    #include <map>
    #include <cmath>
    #include <vector>
    #include <algorithm>
    using namespace std;
    const int N = (1<<14);
    
    int  n , m , st[N<<1] ,top , ans[N<<1] , tail ;
    int eh[N] , et[N<<2] , nxt[N<<2] , tot ;
    bool mark[N<<2];
    
    struct node
    {
        int x , y ;
    }  key[N<<1] , door[N<<1];
    
    void init()
    {
        tot = 0 ;
        memset( eh , -1 , sizeof eh );
        memset( mark ,false , sizeof mark );
    }
    
    void addedge( int u , int v )
    {
        et[tot] = v , nxt[tot] = eh[u] , eh[u] = tot++ ;
        et[tot] = u , nxt[tot] = eh[v] , eh[v] = tot++ ;
    }
    
    bool dfs( int u )
    {
        if( mark[u] ) return true;
        if( mark[u^1] ) return false ;
        mark[u] = true ;
        st[top++] = u ;
        for( int i = eh[u] ; ~i ; i = nxt[i] ){
            int v = et[i];
            if( !dfs(v^1) )  return false;
        }
        return true;
    }
    
    bool solve()
    {
        tail = 0 ;
        for( int i = 0 ; i < 2 * n ; i += 2 ){
            if( !mark[i] && !mark[i+1] ) {
                top = 0 ;
                if( !dfs(i) ){
                    while( top > 0 ) mark[ st[--top] ] = false ;
                    if( !dfs(i+1) ) return false;
                }
            }
            if( mark[i] )
                ans[tail++] = i + 1 ;
            else
                ans[tail++] = i + 2 ;
        }
        return true;
    }
    
    void run()
    {
        int x, y ;
        init();
        for( int i = 0 ; i < m ; ++i ){
            scanf("%d%d",&x,&y);
            x-- , y -- ;
            addedge(x,y) ;
        }
        if( !solve() )puts("NIE");
        else {
            for( int i = 0 ; i < tail ;++i )
                printf("%d
    ",ans[i]);
    
        }
    }
    
    int main()
    {
        #ifdef LOCAL
            freopen("in.txt","r",stdin);
        #endif // LOCAL
        while( ~scanf("%d%d",&n,&m) ) run();
    }
    only strive for your goal , can you make your dream come true ?
  • 相关阅读:
    【Canvas】(1)---概述+简单示例
    【jQuery】(8)---jquery Ajax
    lastlogon
    windows server core 2016 IIS远程管理的那些坑
    开机手机显示存储空间不足某些系统功能可能无法正常使用,而且无法取消这个界面,导致手机停在这个界面无法操作。
    javascript prototype理解
    微信小程序诡异错误this.setData报错
    转:goproxy和go modules的初步使用
    真机调试No target device的解决(android studio)3.4.1
    unable to access android sdk add-on list的解决
  • 原文地址:https://www.cnblogs.com/hlmark/p/4023928.html
Copyright © 2011-2022 走看看