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;}elsecout<<"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;//把这一点染成1color[Other(x)]=2;//把其相对的点染成2cnt++;Ans[cnt]=x;//把这一点放入Ans中,方便后面清空for (int i=0;i<E[x].size();i++)//传递染色if (!dfs(E[x][i]))//如果传递失败,说明矛盾return 0;return 1;
}

转载于:https://www.cnblogs.com/SYCstudio/p/7134050.html

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

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

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

  2. HDU 1814 Peaceful Commission(2-SAT)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1814 题意: 要建立一个和平委员会,要满足以下条件: 每个党派都在委员会中恰有1个代表, ...

  3. HDU 1814 Peaceful Commission

    2-SAT,输出字典序最小的解,白书模板. //TwoSAT输出字典序最小的解的模板 //注意:0,1是一组,1,2是一组..... #include<cstdio> #include&l ...

  4. [HDU 1814] Peaceful Commission

    一.题目 点此看题 二.解法 这就是2-sat\text{2-sat}2-sat带字典序最小解的经典问题,时间复杂度O(n2)O(n^2)O(n2),还是结合代码讲更好: #include <c ...

  5. hdu 1814 Peaceful Commission 题解

    题目传送门 题目大意: 有 nnn 个团队,每个团队两个人,现在要组成一个 nnn 人的组织,要求每个团队中只能有 111 个人在组织里,给出 mmm 组憎恶关系,相互憎恶的两人不能同时在组织里,给出 ...

  6. [2-sat专练]poj 3683,hdu 1814,hdu 1824,hdu 3622,hdu 4115,hdu 4421

    文章目录 Priest John's Busiest Day code Peaceful Commission code Let's go home code Bomb Game code Elimi ...

  7. hdu 1814 字典序最小的2sat(暴力深搜)

    题意:      题意就是最基础的2sat,关系只有矛盾关系,然后二选一,关键是这个题目是输出字典序最小的那组解. 思路:      输出字典序最小,用强连通那个实现不了(起码没看到有人实现),其实我 ...

  8. HDU 1814(染色)

    题目大意 有n个党派,每个党派2个人,这2*n个人之间存在一些敌对关系,现在要从中选出n个人组成一个委员会,要求满足: 1.每个党派中选1个 2.委员会中不存在敌对关系 分析 题目中第i个党派的成员编 ...

  9. hdu 4622 Reincarnation SAM模板题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有Q次区间查询(Q <= 10000),问区 ...

最新文章

  1. 自动化测试selenium+java学习笔记
  2. Linux环境安装python3.6(APT方式)
  3. 陈松松:刚入门的视频营销新人,需要做哪些准备?
  4. mac下完全卸载postgresql的方法
  5. 接到需求之后,产品经理如何高效的从“想”到“做”?
  6. 鸿蒙1号六年级下册课时练答案,【奥数天天练】小学1~6年级思维能力特训|第310期...
  7. B1023 组个最小数 (20分)
  8. 如何解决 React 官方脚手架不支持 Less 的问题
  9. SqlServer整库备份还原脚本
  10. DEVONthink Pro作为浏览器插件脚本,如何使用
  11. Java基础零碎知识点总结(持续补充)
  12. 收集最全的工业软件大集合
  13. 数据库系统课程设计(高校成绩管理数据库系统的设计与实现)
  14. linux表白程序源码,程序员表白程序,开放源码在此!
  15. 科隆OPTIFLUX2100W/4100C分体式电磁流量计维修
  16. 惠普找不到远程服务器,找不到网络打印机是怎么回事?
  17. FPGA中usb-blaster驱动的安装
  18. html5不用reload重置网页,refresh和reload
  19. 助记词(Mnemonics)生成种子,以及Public Key, Private key
  20. Daytime服务器

热门文章

  1. k8s包管理器helm_eShopOnContainers 知多少[10]:部署到 K8S | AKS
  2. 科技感的动态设计方法-1
  3. 从0开始搭建一个战棋游戏的AI(初级教程)
  4. MySQL中的pid与socket是什么?
  5. 梦幻西游:游戏界的宠粉头子?因为玩家联动做奶茶
  6. JavaWeb课程复习资料——用于突击考试总结
  7. 常见Java面试题 程序中如何决定使用 HashMap 还是 TreeMap?
  8. oracle查询排序asc/desc 多列 order by
  9. Oracle数据库管理系统:大数据的备份
  10. Java 内存模型(一)