[POI2001]Peaceful Commission 题解 & 2-SAT题目解法

[POI2001]Peaceful Commission 题解 & 2-SAT题目解法

题目地址:HDUOJ:Problem – 1814

题目描述

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.

有n个党派,每个党派2人,其中有m对人有矛盾无法同时出席会议,要求从每个党派选1人出席会议,求一种字典序最小的方案,如果无解,输出NIE。输入包含多组数据。

输入输出格式

输入格式:
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.

输出格式:
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.

输入输出样例

输入样例#1:

3 2
1 3
2 4

输出样例#1:

1
4
5

题解

本题是2-SAT类题目的裸题,因此下面的讲解也适用于其他2-SAT题目。
对于本题,由于每个党派都是2个人选1个人参加会议,那么如果两个人有矛盾,假设他们是A1和B1,那么如果选择A1则必须选择B2,如果选择B1则必须选择A2(原命题与逆否命题)。我们把这种“必须”给建成图中的有向边。那么就转化为一个图论问题。
对于2-SAT问题的图,我们采取枚举-检验的方式寻找可行解。我们从每一个可能的情况入手,假如这个党派的答案当前没有被确定,那么首先检查选择A1是否可行,如果不可行再检查选择A2(因为本题要求输出字典序最小的解)。对于检查,我们可以使用DFS,如果发现选择A1的这条链上已经存在有被标记“不可选择”的点,那么选择A1是不可行的,因为图中的边都表示“必须”的含义。尝试失败后,要记得将此次尝试涉及的点的状态还原。
另外,当点较多的时候,可以使用Tarjan-SCC缩点后在缩点图上进行上述操作。这种优化方法可能会在之后的题目中讲解。

代码

以下代码借鉴了刘汝佳、陈锋著《算法竞赛入门经典 训练指南》一书的写法。

// Code by KSkun, 2018/3
#include <cstdio>
#include <cstring>

#include <vector>

const int MAXN = 16005;

std::vector<int> gra[MAXN], ans;
int n, m, ut, vt;
bool mark[MAXN];

inline bool dfs(int x) {
    if(mark[x ^ 1]) return false;
    if(mark[x]) return true;
    mark[x] = true;
    ans.push_back(x);
    for(int i = 0; i < gra[x].size(); i++) {
        if(!dfs(gra[x][i])) return false;
    }
    return true;
}

inline bool work() {
    memset(mark, 0, sizeof(mark));
    for(int i = 0; i < n << 1; i += 2) {
        if(!mark[i] && !mark[i ^ 1]) {
            ans.clear();
            if(!dfs(i)) {
                for(int j = 0; j < ans.size(); j++) {
                    mark[ans[j]] = false;
                }
                ans.clear();
                if(!dfs(i ^ 1)) return false;
            }
        }
    }
    return true;
}

int main() {
    while(scanf("%d%d", &n, &m) != EOF) {
        for(int i = 0; i < n << 1; i++) {
            gra[i].clear();
        }
        for(int i = 1; i <= m; i++) {
            scanf("%d%d", &ut, &vt); ut--; vt--;
            gra[ut].push_back(vt ^ 1);
            gra[vt].push_back(ut ^ 1);
        }
        if(work()) {
            for(int i = 0; i < n << 1; i += 2) {
                if(mark[i]) printf("%d\n", i + 1);
                else printf("%d\n", (i ^ 1) + 1);
            }
        } else {
            puts("NIE");
        }
    } 
    return 0;
}


发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据