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. 诊断:Goldengate OGG-01163 Bad column length

    故障现象: OGG- Bad column length () specified . 原因:源端修改了字段长度.虽然源端和目标端的长度已经通过DDL语句修改到一致,在extract进程未重启的情况下 ...

  2. 2n皇后 - 回溯

    题目地址:http://www.51cpc.com/web/problem.php?id=1172 Summarize: 1. 递归回溯: 2. 先扫完一种皇后,再扫描另一种: 3. 循环输入: 4. ...

  3. Wind rotor states

    test test Table of Contents 1. Wind rotor states 1.1. Turbulent Wake State 1.2. Vortex Ring State 1. ...

  4. * format-- set command window output display format

    the displayed number may not match the input number due to display format default: 4 decimal syntax: ...

  5. 自定义Realm

    [单Realm] 1) jar包 2) 实现自定义Realm public class RealmOne implements Realm{ /** * 获取基本类名 */ @Override pub ...

  6. MySql 执行计划解读

    说明 解读执行计划l对于我们日常工作中慢sql的分析和调优有很大帮助,同时在解读的过程中也能知道如何规避慢sql 建议需要了解join匹配原理的知识:https://www.cnblogs.com/L ...

  7. Eclipse不编译解决方案

    原文链接:http://blog.csdn.net/huahuagongzi99999/article/details/7719882    转来自己用 这两天Eclipse 不编译了,无论怎么更改保 ...

  8. hdu 4670 树的分治-求点对的个数

    /* 树的分治 因为树的点权值可达到10^15,注意手动扩栈,还有int64 题意:给你一棵树,给你一些素数,给你每个点一个权值且每个权值均可由这些素数组成.现在定义任意任意两点的价值为他们路径上的权 ...

  9. Codeforces Round #403(div 2)

    A =w= B 题意:一个数轴上有n个整点,每个点都有一个速度,选一个点让他们集合,使得时间最少. 分析: 直接三分 C 题意:给定一棵树,任意两个距离小等于二的点不能染相同的颜色,求最小颜色数和染色 ...

  10. RDLC报表总结

    这2天纠结的报表基本上已近完成大部分功能.现在总结一下自己近期的学习成果 首先制作微软RDLC报表由以下三部分构成:1.制作自己的DateSet集合(就是报表的数据集):2.制作自己的报表文件.rdl ...