HDU1814(2-SAT)
Peaceful Commission
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4180 Accepted Submission(s): 1395
Problem Description
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
There are multiple test cases. Process to end of file.
Output
Sample Input
1 3
2 4
Sample Output
4
5
Source
//2017-08-28
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector> using namespace std; const int N = ;
const int M = ;
const int INF = 0x3f3f3f3f;
int head[N], tot;
struct Edge{
int to, next;
}edge[M]; void init(){
tot = ;
memset(head, -, sizeof(head));
} void add_edge(int u, int v){
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
} bool book[N];
vector<int> vec;//保存dfs过程中经过的点 //input: u 顶点
//output: true 从u开始染色,不会出现NOT u和u染为同一种颜色; false dfs染色失败
bool dfs(int u){
if(book[u^])return false;//表示染到非u,染色失败
if(book[u])return true;
book[u] = true;
vec.push_back(u);
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to;
if(!dfs(v))
return false;
}
return true;
} //input:n 图的顶点数
//output:true 存在可行解; false 不存在可行解
bool twoSAT(int n){
memset(book, , sizeof(book));
for(int u = ; u < n; u += ){
if(book[u] || book[u^])continue;
vec.clear();
if(!dfs(u)){//如果选u不成功,把dfs过程中的点都从答案中删去
for(int i = ; i < vec.size(); i++)
book[vec[i]] = ;
vec.clear();
if(!dfs(u^))return false;//如果选NOT u也不成功,说明不存在可行解
}
}
return true;
} int n, m; int main()
{
std::ios::sync_with_stdio(false);
//freopen("inputG.txt", "r", stdin);
while(cin>>n>>m){
init();
int u, v;
while(m--){
cin>>u>>v;
u--; v--;
add_edge(u, v^);// u -> NOT v
add_edge(v, u^);// v -> NOT u
}
if(twoSAT(n<<)){
for(int i = ; i < (n<<); i++)//字典序输出解
if(book[i])
cout<<i+<<endl;
}else cout<<"NIE"<<endl;
}
return ;
}
HDU1814(2-SAT)的更多相关文章
- 多边形碰撞 -- SAT方法
检测凸多边形碰撞的一种简单的方法是SAT(Separating Axis Theorem),即分离轴定理. 原理:将多边形投影到一条向量上,看这两个多边形的投影是否重叠.如果不重叠,则认为这两个多边形 ...
- 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 ...
- 2—sat
模型的解决方法看论文<利用对称性解决2-SAT问题> HDU1814 :难度1.5 HDU1824: 难度 2 HDU1815: 难度3 HDU1816: 对于每两个人,二选一HDU181 ...
- hdu1814 Peaceful Commission
hdu1814 Peaceful Commission 题意:2-sat裸题,打印字典序最小的 我写了三个 染色做法,正解 scc做法,不管字典序 scc做法,错误的字典序贪心 #include &l ...
- Map Labeler POJ - 2296(2 - sat 具体关系建边)
题意: 给出n个点 让求这n个点所能建成的正方形的最大边长,要求不覆盖,且这n个点在正方形上或下边的中点位置 解析: 当然是二分,但建图就有点还行..比较难想..行吧...我太垃圾... 2 - s ...
- HDU1814 Peaceful Commission 2-sat
原文链接http://www.cnblogs.com/zhouzhendong/p/8099115.html 题目传送门 - HDU1814 题面 Description 根据宪法,Byteland民 ...
- HDU3062&&HDU1814
Preface 两道2-SAT模板题. HDU3062 看题目就一眼2-SAT.一对夫妻看成一个变量,之间的矛盾可以看成限制. 考虑不同席的限制,相当于选了\(i\)就不选\(j\),即必选\(j'\ ...
- 学习笔记(two sat)
关于two sat算法 两篇很好的论文由对称性解2-SAT问题(伍昱), 赵爽 2-sat解法浅析(pdf). 一些题目的题解 poj 3207 poj 3678 poj 3683 poj 3648 ...
- LA 3211 飞机调度(2—SAT)
https://vjudge.net/problem/UVALive-3211 题意: 有n架飞机需要着陆,每架飞机都可以选择“早着陆”和“晚着陆”两种方式之一,且必须选择一种,第i架飞机的早着陆时间 ...
- HIT 1917 2—SAT
题目大意:一国有n个党派,每个党派在议会中都有2个代表, 现要组建和平委员会,要从每个党派在议会的代表中选出1人,一共n人组成和平委员会. 已知有一些代表之间存在仇恨,也就是说他们不能同时被选为和平委 ...
随机推荐
- Python大黑阔—url采集+exp验证,带你批量测试
i春秋作家:大木瓜 前言: 最近几天在整理从各处收集来的各种工具包,大大小小的塞满了十几个G的硬盘,无意间发现了一个好几年前的0day.心血来潮就拿去试了一下,没想到真的还可以用,不过那些站点都已经老 ...
- Python建代理配合burp实现自动免费用水
i春秋作家:凉风有信 Hi , 我是凉风 半年不见了啊,我又回来了因为太菜都不好意思发文章了 0×00 目录 0×01 前言0×02 利用fd分析与实战0×03 Python+burp简易搭建代理服务 ...
- spring-boot集成thymeleaf。
thymeleaf是前台页面展示,原来一直是jsp,jsp中包含很多服务器端的逻辑,逐渐淘汰.同样功能的还有freemarker.孰好孰坏不予评价,只做简单实现. 1.基本思路 (1)pom.xml中 ...
- flask的变量和函数
flask 中有内置的的变量函数 ,那些特殊的变量可以实现某些功能 config :可以从模板中直接访问Flask当前的config对象:{{config.SQLALCHEMY_DATABASE_UR ...
- [LeetCode] 反转整数
题目: 给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 注 ...
- 【转载】InstallShield 生成安装日志
msiexec.exe /p "C:\Documents and Settings\qa\Desktop\8.3 Patch.msp" /l*v c:\log85gold.log ...
- EFCore.MySql当模型遇到int[]怎么办
我使用的是Pomole.EntityFrameworkCore.MySql 需要将旧项目中的excels表转成实体,其中有一列是json格式的int[] 当遇到第一张表的时候,我使用了这样的方法来读取 ...
- (转)关于python3中staticmethod(静态方法)classmethod(类方法)实例方法的联系和区别
原文:http://dmcoders.com/2017/08/30/pythonclass/ https://zhuanlan.zhihu.com/p/28010894------正确理解Python ...
- hadoop2.7的目录结构
1.$HADOOP_HOME/bin目录下文件及作用 文件名称 说明 hadoop 用于执行hadoop脚本命令,被hadoop-daemon.sh调用执行,也可以单独执行,一切命令的核心 2.$HA ...
- dex内存提取
转 http://blog.csdn.net/asmcvc/article/details/18216531 智能手机的普及将移动互联网的发展推到了一个让所有人都为之兴奋的高度,我想即使是以商业眼光见 ...