hdoj 1814 Peaceful Commission 【2-sat暴力染色 求解字典序最小解】
来源:互联网 发布:批量产生随机数软件 编辑:程序博客网 时间:2024/06/05 23:41
Peaceful Commission
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2343 Accepted Submission(s): 696
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.
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.
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 21 32 4
Sample Output
145
题意:
N个党派要成立一个委员会,此委员会必须满足下列条件:
- 每个党派都在委员会中恰有1个代表,
- 如果2个代表彼此厌恶,则他们不能都属于委员会。
每个党在议会中有2个代表。代表从1编号到2n。 编号为2i-1和2i的代表属于第I个党派。
现给出M个矛盾关系,问你该委员会能否创立?若不能输出NIE,若能够创立输出字典序最小的解。
dfs暴力:本想改下实现过程,结果一直WA。。。
#include <cstdio>#include <cstring>#include <stack>#include <queue>#include <vector>#include <algorithm>#define MAXN 40000+10#define MAXM 200000+10#define INF 1000000using namespace std;struct Edge{int from, to, next;}edge[MAXM];int head[MAXN], edgenum;stack<int> S;bool mark[MAXN];//标记是否为真 int N, M;void init(){edgenum = 0;memset(head, -1, sizeof(head));memset(mark, false, sizeof(mark));} void addEdge(int u, int v){Edge E = {u, v, head[u]};edge[edgenum] = E;head[u] = edgenum++; }void getMap(){int a, b;while(M--){scanf("%d%d", &a, &b);a--, b--;addEdge(a, b ^ 1);addEdge(b, a ^ 1);}}bool dfs(int u){if(mark[u ^ 1]) return false;if(mark[u]) return true;mark[u] = true;S.push(u);for(int i = head[u]; i != -1; i = edge[i].next){int v = edge[i].to;if(!dfs(v)) return false;}return true;}bool solve(){for(int i = 0; i < 2*N; i+=2)//共N组 {if(!mark[i] && !mark[i ^ 1])//还没有判定 {while(!S.empty())//用STL的话 注意清空栈 {S.pop();}if(!dfs(i)){while(!S.empty()){int v = S.top();S.pop();mark[v] = false;}if(!dfs(i ^ 1))//矛盾 必无解 return false;}}}return true;}int main(){while(scanf("%d%d", &N, &M) != EOF){init();getMap();if(solve()){for(int i = 0; i < 2*N; i++)if(mark[i]) printf("%d\n", i+1);}elseprintf("NIE\n");}}
0 0
- hdoj 1814 Peaceful Commission 【2-sat暴力染色 求解字典序最小解】
- [2-SAT 字典序最小解 暴力dfs 模板题] HDU 1814 Peaceful Commission
- 【2-SAT字典序最小解】HDU1814[Peaceful Commission]题解
- HDOJ 1814 - Peaceful Commission 输出字典序最小的2-sat方案...暴力2-sat
- HDU 1814 Peaceful Commission(2-sat 输出字典序最小解 )
- HDU 1814:Peaceful Commission(2-SAT的字典序最小解)
- HDU 1814 Peaceful Commission (2-sat暴力染色)
- hdu 1814 Peaceful Commission(2-sat 染色)
- 【2-sat】 HDOJ 1814 Peaceful Commission
- HDU 1814 Peaceful Commission(2-SAT:最小字典序)
- hdu 1814 Peaceful Commission 2-sat 按字典序输出
- HDU 1814Peaceful Commission (最小字典序 2-SAT)
- 2-SAT DFS暴力求最小字典序的模板(Peaceful Commission)
- hdu 1814 Peaceful Commission (2-sat 输出字典序最小路径)
- Hdu 1814 Peaceful Commission(2-sat+输出字典序最小方案)
- hdu 1814 Peaceful Commission(2-SAT 输出字典序最小的解)
- HDU 1814 Peaceful Commission(2-sat 模板题输出最小字典序解决方案)
- HDU 1814 Peaceful Commission 2-sat(字典序最小的解)
- 19岁少年重塑CPU,降低80%计算机功耗
- pip安装使用详解
- 链表系列
- oracle登录错误(ORA-01033:ORACLE initialization or shutdown in progress
- Java Heap sapce 与 Java PermGen space
- hdoj 1814 Peaceful Commission 【2-sat暴力染色 求解字典序最小解】
- linux qt5.5发布 依赖库
- SylixOS优先级与POSIX优先级关系
- AngularJS学习笔记(二)
- [LeetCode]Implement Trie (Prefix Tree)
- 百度云推送的ios证书配置
- NYOJ 638 小媛在努力(水)
- Android开发新手需知EditText属性解析
- C++ 继承与接口 知识点 小结(一)