Peaceful Commission
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6868 Accepted Submission(s): 2126
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
题目大意 : 有N对人, 需要从这 N 对 当中, 每一对各选一个人, 但是添加一些限制条件, 比如谁和谁不能同时选, 如果存在满足的情况, 输出字典序最小的, 否则, 输出 NIE
思路 : 这道题主要考的是2-SAT的打印, 正常求的话是建完图后判环, 本题由于需要输出字典序最小, 所以用到一个方法叫暴力染色(一开始也不会,看了别人的思路理解了才做对)。 对于一对点 U, V, 若存在U 可以走到 V, 那么就不能选择从U 走了, 因为连边代表必须选, 一对点只能选一次, 这时候就要选另一个点, 若另一个点也不能选, 说明出现环了, 直接退出就好, 关键在于怎么判断U是否可选, 可以类似于tarjan一样, 通过一个栈, 来实现存储和删除的操作, 如果该点可以选, 那么就不动, 否则将本次扫到的所有点全部清0, 看另一个点是否可以满足条件。
Accepted code
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e4 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
vector <int> e[MAXN << 1];
int n, m;
stack <int> st;
bool vis[MAXN << 1];
void init() {
for (int i = 0; i < 2 * n; i++) e[i].clear(), vis[i] = false;
}
bool dfs(int x) {
if (vis[x ^ 1]) return false; // 选到相邻的点, 直接退出
if (vis[x]) return true;
vis[x] = true, st.push(x);
for (int i = 0; i < SZ(e[x]); i++) {
int vi = e[x][i];
if (!dfs(vi)) return false;
}
return true;
}
bool check() {
for (int i = 0; i < 2 * n; i += 2) {
if (!vis[i] && !vis[i ^ 1]) {
while (!st.empty()) st.pop();
if (!dfs(i)) {
while (!st.empty()) { // 一个点不可选清空
int k = st.top(); st.pop();
vis[k] = false;
}
if (!dfs(i ^ 1)) return false; // 两个点都不可选
}
}
}
return true;
}
int main()
{
while (~sc("%d %d", &n, &m)) {
init();
for (int i = 0; i < m; i++) {
int ui, vi;
sc("%d %d", &ui, &vi);
ui--, vi--;
e[ui].push_back(vi ^ 1);
e[vi].push_back(ui ^ 1);
}
if (check()) {
for (int i = 0; i < 2 * n; i += 2) {
if (vis[i]) printf("%d\n", i + 1); // vis表示选择
else printf("%d\n", i + 2);
}
}
else printf("NIE\n");
}
return 0;
}