码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1814 Peaceful Commission

时间:2014-10-14 14:38:38      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   os   ar   java   for   

 

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\n",ans[i]);

    }
}

int main()
{
    #ifdef LOCAL
        freopen("in.txt","r",stdin);
    #endif // LOCAL
    while( ~scanf("%d%d",&n,&m) ) run();
}

 

HDU 1814 Peaceful Commission

标签:des   style   blog   color   io   os   ar   java   for   

原文地址:http://www.cnblogs.com/hlmark/p/4023928.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!