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

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 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

Input

In the first line 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.

Output

The text 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 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

Http

HDU:https://vjudge.net/problem/HDU-1814

HIT:https://vjudge.net/problem/HIT-1917

CJOJ:http://oj.changjun.com.cn/problem/detail/pid/1288

Source

2-sat

翻译(By CJOJ)

根据宪法,Byteland民主共和国的公众和平委员会应该在国会中通过立法程序来创立。 不幸的是,由于某些党派代表之间的不和睦而使得这件事存在障碍。

此委员会必须满足下列条件:

每个党派都在委员会中恰有1个代表,

如果2个代表彼此厌恶,则他们不能都属于委员会。

每个党在议会中有2个代表。代表从1编号到2n。 编号为2i-1和2i的代表属于第I个党派。

任务

写一程序:

输入党派的数量和关系不友好的代表对,

计算决定建立和平委员会是否可能,若行,则列出委员会的成员表。

题目大意

有n个组,每组里有两个元素,现在给出m对元素不能都选择的条件,求一个集合使得每组里面恰好选择了一个元素且满足上述m对条件

解决思路

这是一道2-sat的模板题。

我们设i和i'表示一个党派中的两个人,那么如果i与j不能够共存,则说明若选i则必须选j',同理,若选j则必须选择j'。由此,我们可以建边连图。对于互相厌恶的两个代表i,j连边i->j'j->i'。注意这是有向边

为什么是有向边呢?

因为i与j互相厌恶不代表i'与j'互相厌恶,如果连了边j'->i,那么意思是选j'必须选i(同时也表示一定不能选i'),但j'与i并不一定互相厌恶,所以连的边一定是有向边。

然后就是判断的方法。因为题目要求要按字典序输出,所以对于每一组,我们先检查标号较小的那个(i),如果不行,再检查编号大的(i+1)

另外一点需要注意的是,由于要求字典序输出,所以不能使用Tarjan缩点+拓扑排序的方法,只能用dfs一个一个判断

代码

//暴力染色法
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
using namespace std; #define Other(x) ((x%2==0) ? (x-1) : (x+1) ) //Other(x) 表示与x同党派的另一个人。这里用i和i+1来表示同党派的两个人(i为奇数) const int maxN=16050;
const int inf=2147483647; int n,m;
int cnt;
vector<int> E[maxN];//用来存放边
int color[maxN];//存染色时每个点的颜色,0代表还没有填,1和2分别代表两种颜色
int Ans[maxN];//临时存放答案 bool solve();//循环染色
bool dfs(int x);//检查是否满足没有矛盾 int main()
{
while (cin>>n>>m)
{
n=n*2;
int a,b;
for (int i=1;i<=n;i++)
E[i].clear();//因为HDU上是多组数据,所以每一次都要重新清空
for (int i=1;i<=m;i++)
{
cin>>a>>b;
E[a].push_back(Other(b));
E[b].push_back(Other(a));
}
if (solve())
{
for (int i=1;i<=n;i++)//输出所有被染成1色的点
if (color[i]==1)
cout<<i<<endl;
}
else
cout<<"NIE"<<endl;
}
} bool solve()
{
memset(color,0,sizeof(color));
for (int i=1;i<=n;i++)
{
if (color[i]!=0)//如果这个点已经被染色(即前面已经给其染过色)
continue;
cnt=0;//临时保存答案的计数器
if (!dfs(i))//先尝试把i染成1,若不行则在下面选择Other(i)
{
for (int j=1;j<=cnt;j++)//若要选择Other(i)则要把之前检查i是否满足时用到的数组清空
{
color[Ans[j]]=0;
color[Other(Ans[j])]=0;
}
cnt=0;//感谢dsl大佬的查错,这里要加cnt=0,虽然说不加程序并不会出错,但是会浪费一堆空间,若数据大时可能会出问题
if (!dfs(Other(i)))//如果把Other(i)也染成1也不满足,说明无解
return 0;
}
}
return 1;
} bool dfs(int x)
{
if (color[x]==1)//如果该点已被染成1,说明满足并返回
return 1;
if (color[x]==2)//如果该点已被染成2,说明矛盾
return 0;
color[x]=1;//把这一点染成1
color[Other(x)]=2;//把其相对的点染成2
cnt++;
Ans[cnt]=x;//把这一点放入Ans中,方便后面清空
for (int i=0;i<E[x].size();i++)//传递染色
if (!dfs(E[x][i]))//如果传递失败,说明矛盾
return 0;
return 1;
}

HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题)的更多相关文章

  1. HIT 1917 Peaceful Commission

    这道题题意就是给你n对人,一对中编号为x,x+1,给你m对矛盾,表示这两个人不能同时选. 然后就是Two-Sat的模板题了,就是根据对称性,连边,加缩点,最后拓扑排序,求出一组可行解就可以了. #in ...

  2. hdu 2191 悼念512汶川大地震遇难同胞 【多重背包】(模板题)

    题目链接:https://vjudge.net/problem/HDU-2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活                                   ...

  3. hdu 1232 变成生成树至少还要加几条边 (并查集模板题)

    求一个图 变成生成树至少还要加几条边(成环的边要删掉,但不用统计) Sample Input4 2 //n m1 3//u v4 33 31 21 32 35 21 23 5999 00 Sample ...

  4. HDU 1814 Peaceful Commission(2-sat 模板题输出最小字典序解决方式)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1814 Problem Description The Public Peace Commission ...

  5. hdu 1814 2-sat 输出字典最小的和任意序列的 模板题

    /* 思路:http://blog.csdn.net/string_yi/article/details/12686873 hdu 1814 输出字典序最小的2-sat */ #include< ...

  6. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  7. HDU 1251 Trie树模板题

    1.HDU 1251 统计难题  Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #incl ...

  8. HDU 4280:Island Transport(ISAP模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...

  9. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

随机推荐

  1. JVM学习笔记三:垃圾收集器与内存分配策略

    内存回收与分配重点关注的是堆内存和方法区内存(程序计数器占用小,虚拟机栈和本地方法栈随线程有相同的生命周期). 一.判断对象是否存活? 1. 引用计数算法 优势:实现简单,效率高. 致命缺陷:无法解决 ...

  2. 第一天上午——HTML网页基础知识以及相关内容

    今天上午学习了HTML基础知识以及相关内容,还有DW的基本使用方法. HTML(HyperText Markup Language):超文本标记语言,超文本:网页中除了包含文本文字之外,还包含了图片, ...

  3. C++抽象编程·运算符重载与友元函数

    运算符重载(Operator overloading) 从我们在几个前篇的类的层次介绍中可以知道,C++可以扩展标准运算符,使其适用于新类型.这种技术称为运算符重载. 例如,字符串类重载+运算符,使其 ...

  4. 排序与检索【UVa10474】Where is the Marble?

    Where is the Marble?  DescriptionRaju and Meena love to play with Marbles. They have got a lot of ma ...

  5. openvpn实现内网 映射到 外网

    openvpn实现内网 映射到 外网 场景介绍: 机器介绍 本地一台Ubuntu服务器A , 处于内网中 , 无外网IP 外网一台Ubuntu服务器B , 外网地址139.199.4.205 目标 : ...

  6. SpringMVC 整合Jackson报错

    最近用spring4.x整合Jackson,结果莫名其妙的一直报错,网上收索的结果都是在maven或者gradle的环境下配置依赖条件解决的:但是eclipseIDE环境下的jar包应该是会自动依赖影 ...

  7. vue-resource promise兼容性问题

    背景 其实这个问题在之前的项目开发中就出现过,但是当初只解决问题了,并没有针对问题作总结:于是乎今天踩到了自己埋的坑,所以决定记录一下.那么到底是什么问题呢?就是"在安卓低版本,如果你在vu ...

  8. Jmeter元件运行顺序

    JMeter执行顺序逻辑如下: 1.配置元件(如果存在) 2.前置处理器(如果存在) 3.定时器(如果存在) 4.取样器(如果存在) 5.后置处理器(如果存在且取样器的结果不为空) 6.断言(如果存在 ...

  9. 【Appnium+C#+Winform自动化测试系列】前言

    目录(后期持续更新) 一.前言  1.为什么选择Appnium 最近这些年APP保持着持续的火热,对应的APP测试行业也是跟着水涨船高.由于市场的需求,APP测试平台也涌出大量的自动化测试工具. 在对 ...

  10. Swift json字典转模型 项目记录

    背景 最近项目开始转用Swift3开发,由于Swift中json(字典)转模型的选择方案较多,笔者最开始选择了HandyJSON的方案,在使用一段时间后发现当要进行某个字段取值使用时需要进行各种的转化 ...