Peaceful Commission

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

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
 
Source
 
 
/*
最小字典序
直接暴力枚举DFS,首先将所有的点都置为未染色,然后从第一个点开始DFS染色,我们先尝试将i染
成1(答案中的颜色),将~i染成2,然后dfs i的所有后继并染色,如果对于后继j没有染色,那么将j然
后为1,~j染成2。如果后继j已经被染成2,则说明不能选则i,如果j已经染成1,则说明可以。
那么这些后继就可以被选择。如果选择i的时候失败了,那么必定要选择~i,如果也失败,则说明无解。否则
按次序选取下一个未被染色的点。时间复杂度O(nm)。
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 16001 using namespace std;
int col[maxn],head[maxn],ans[maxn];
int a,b,n,m,num,tot,cnt;
struct node
{
int u,v,next;
}e[maxn<<]; inline void add(int u,int v)
{
e[++num].u=u;
e[num].v=v;
e[num].next=head[u];
head[u]=num;
} bool dfs(int u)
{
if(col[u]==) return true;
if(col[u]==) return false;
col[u]=;col[u^]=;ans[cnt++]=u;
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].v;
if(!dfs(v)) return false;
}
return true;
} bool solve()
{
memset(col,,sizeof col);
for(int i=;i<n;i++)
{
if(col[i]) continue;
cnt=;
if(!dfs(i))
{
for(int j=;j<cnt;j++)
{
col[ans[j]]=;
col[ans[j]^]=;
}
if(!dfs(i^))return false;
}
}
return true;
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
n<<=;num=;
memset(head,,sizeof head);
while(m--)
{
scanf("%d%d",&a,&b);
a--;b--;
add(a,b^);add(b,a^);
}
if(solve())
{
for (int i=;i<n;i++)
if(col[i]==)
printf("%d\n",i+);
}
else printf("NIE\n");
}
return ;
}

hdu1814Peaceful Commission(2-SAT)的更多相关文章

  1. 【2-SAT(最小字典序/暴力染色)】HDU1814-Peaceful Commission

    [题目大意] 和平委员会每个党派有2个人,只能派出其中1个,其中有一些人之间互相讨厌不能同时派出.求出派遣方案,如果有多种方案输出字典序最小的方案. [思路] 最小字典序只能用暴力染色.初始时均没有染 ...

  2. HDU1814Peaceful Commission求2-sa最小字典序

    #include <iostream> #include <cstdio> #include <vector> #include <cstring> # ...

  3. 初涉2-SAT

    2-SAT:有趣的图论模型 什么是2-SAT SAT是适定性(Satisfiability)问题的简称.之所以研究2-sat是因为当k>2时,k-sat问题已经被证明是NPC的了. 2-sat问 ...

  4. Peaceful Commission

    Peaceful Commission Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...

  5. 多边形碰撞 -- SAT方法

    检测凸多边形碰撞的一种简单的方法是SAT(Separating Axis Theorem),即分离轴定理. 原理:将多边形投影到一条向量上,看这两个多边形的投影是否重叠.如果不重叠,则认为这两个多边形 ...

  6. POJ 3678 Katu Puzzle(2 - SAT) - from lanshui_Yang

    Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a ...

  7. hdu 1814 Peaceful Commission (2-sat 输出字典序最小的路径)

    Peaceful Commission Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  8. HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题)

    HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题) Description T ...

  9. hdu1814 Peaceful Commission

    hdu1814 Peaceful Commission 题意:2-sat裸题,打印字典序最小的 我写了三个 染色做法,正解 scc做法,不管字典序 scc做法,错误的字典序贪心 #include &l ...

随机推荐

  1. dorado 7 使用总结

    最近项目上需要,使用了dorado 7 ,总体感觉还可以,快速开发很方便,然而在方便的同时,难免有些太过繁琐,很多东西都封装了起来,会造成很多不便.因此快速开发的项目可以使用,其它的不推荐.现在打算将 ...

  2. 洛谷——P1516 青蛙的约会

    P1516 青蛙的约会 题目描述 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件 ...

  3. Linux下“任务管理器”

    也不知道linux叫不叫任务管理器. Ctrl+Alt+T打开终端,输入top,就会出现一堆东西. 如果有个东西未响应了,就可以输入k+这个进程的pid就可以杀死它. https://blog.csd ...

  4. highcharts图表的常见操作

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  5. 【3】数据筛选3 - BeautifulSoup4

    #目录     1. 开发前准备     2. 不同解析器对比     3. BeautifulSoup4 初始化和节点对象的认识     4. BS4 案例操作:初始化对象文档     5. 节点查 ...

  6. reading/writing files in Python

    file types: plaintext files, such as .txt .py Binary files, such as .docx, .pdf, iamges, spreadsheet ...

  7. Leetcode 86.分隔链表

    分隔链表 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = 1-> ...

  8. Linux & Filesystem Hierarchy Standard

    Linux & Filesystem Hierarchy Standard The Filesystem Hierarchy Standard of Linux https://zhuanla ...

  9. spring boot & JsonParser

    spring boot https://stackoverflow.com/questions/29313687/trying-to-use-spring-boot-rest-to-read-json ...

  10. HDU 1203 背包问题

    题目大意: 根据学校的申请费用,根据已有的钱得到最大的offer率 这里很明显就是一个价值为概率的背包问题 计算两个offer合并的概率 为a + b - a*b #include <cstdi ...