HDU1814 2-sat 模板
Peaceful Commission
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3378 Accepted Submission(s): 1110
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.
There are multiple test cases. Process to end of file.
4
5
//2-sat模板,白书323页。点的编号从0开始
//输出字典序最小的结果,模板跑出来就是字典序最小的。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=;
/********************** 2-sat模板 **********************/
struct Twosat{
int n;
vector<int> g[maxn*];
bool mark[maxn*];
int s[maxn*],c;
bool dfs(int x){
if(mark[x^]) return false;
if(mark[x]) return true;
mark[x]=true;
s[c++]=x;
for(int i=;i<(int)g[x].size();i++)
if(!dfs(g[x][i])) return false;
return true;
}
void init(int n){
this->n=n;
for(int i=;i<n*;i++) g[i].clear();
memset(mark,,sizeof(mark));
}
void add_clause(int x,int y){//这个函数随题意变化
g[x].push_back(y^);//选了x就必须选y^1
g[y].push_back(x^);
}
bool solve(){
for(int i=;i<n*;i+=)
if(!mark[i]&&!mark[i+]){
c=;
if(!dfs(i)){
while(c>) mark[s[--c]]=false;
if(!dfs(i+)) return false;
}
}
return true;
}
};
/*********************** 2-sat模板 ************************/
int main(){
int n,m,a,b;
Twosat solver;
while(~scanf("%d%d",&n,&m)){
solver.init(n);
for(int h=;h<m;h++){
scanf("%d%d",&a,&b);//a,b不能同时选
a--;b--;
solver.add_clause(a,b);
}
if(solver.solve()){
for(int i=;i<n*;i++)
if(solver.mark[i]) printf("%d\n",i+);
}
else printf("NIE\n");
}
return ;
}
HDU1814 2-sat 模板的更多相关文章
- 2 - sat 模板(自用)
2-sat一个变量两种状态符合条件的状态建边找强连通,两两成立1 - n 为第一状态(n + 1) - (n + n) 为第二状态 例题模板 链接一 POJ 3207 Ikki's Story IV ...
- HDU1814(Peaceful Commission) 【2-SAT DFS暴力求最小字典序的模板】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1814 题意:给出一个数n,代表有n个党派,每个党派要求派出其中一个人去参加会议,且只能派出一人.给出m ...
- 2-SAT模板
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAABcAAAAXCAYAAADgKtSgAAAFCUlEQVR42n2VeVCUdRzGf3n0T/80ld
- TwoSAT算法模板
该模板来自大白书 [解释] 给多个语句,每个语句为“ Xi为真(假) 或者 Xj为真(假)” 每个变量和拆成两个点 2*i为假, 2*i+1为真 “Xi为真 或 Xj为真” 等价于 “Xi为假 –& ...
- 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 ...
- C++ 模板基础
我们学习使用C++,肯定都要了解模板这个概念.就我自己的理解,模板其实就是为复用而生,模板就是实现代码复用机制的一种工具,它可以实现类型参数化,即把类型定义为参数:进而实现了真正的代码可重用性.模版可 ...
- HDU3062&&HDU1814
Preface 两道2-SAT模板题. HDU3062 看题目就一眼2-SAT.一对夫妻看成一个变量,之间的矛盾可以看成限制. 考虑不同席的限制,相当于选了\(i\)就不选\(j\),即必选\(j'\ ...
- (模板)poj2947(高斯消元法解同余方程组)
题目链接:https://vjudge.net/problem/POJ-2947 题意:转换题意后就是已知m个同余方程,求n个变量. 思路: 值得学习的是这个模板里消元用到lcm的那一块.注意题目输出 ...
- 虚拟化技术之kvm镜像模板制作工具virt-sysprep
virt-sysprep这个工具来自libguest-tools这个工具包,它能够把kvm虚拟机对应的磁盘文件做成一个模板,后续我们启动虚拟机就可以基于这个镜像模板启动:什么是镜像模板呢?所谓模板就是 ...
随机推荐
- opencv-学习笔记(2)
opencv-学习笔记(2) 这章记录了 获取像素点,改变像素点 获取图像的属性(行,列,通道数,数据类型) roi感应区 拆分以及合并图像通道 边缘扩充 opencv获取像素点,改变像素点 ---- ...
- CDH问题集
1.在CM中添加主机报JDK错误 手动在机器上安装oracle-jdk1.7+update64.然后在CM中选择不安装oracle-jdk即可. 2.HostMoinitor无法与server联系 查 ...
- C Program基础-宏定义
写好c语言,漂亮的宏定义是非常重要的,我们在阅读别人工程时,往往能看到大量的宏定义,宏定义可以增加代码的可读性,也能增加代码的可移植性,一个好的宏定义甚至是一件艺术品.今天我们就来看看宏定义的方方面面 ...
- Python高级编程-多线程
(一)进程线程概述: 很多同学都听说过,现代操作系统比如Mac OS X,UNIX,Linux,Windows等,都是支持“多任务”的操作系统. 什么叫“多任务”呢?简单地说,就是操作系统可以同时运行 ...
- Swift-map()跟flatMap()区别
map()方法介绍 map() 是 Array 提供的方法,通过接收一个函数作为传入参数,对数组中每个元素进行函数变换得到新的结果值.这样只需要提供 X->Y 的映射关系,就能将数组 [X ...
- JS DOM(2017.12.28)
一.获得元素节点的方法 document.getElementById() 根据Id获取元素节点 document.getElementsByName() 根据name获取元素节点 遍 ...
- ZOJ 2072 K-Recursive Survival
https://vjudge.net/contest/67836#problem/K n people numbered 1 to n around a circle, we eliminate ev ...
- 微信小程序 功能函数 计时器
let lovetime = setInterval(function () { let str = '(' + n + ')' + '重新获取' that.setData({ getText2: s ...
- cmd批处理中set /a和set /p的区别介绍
在 SET 命令中添加了两个新命令行开关: SET /A expression SET /P variable=[promptString]/p 是让你输入/a 是指定一个变量等于一串运算字符 什么参 ...
- Java接口成员变量
定义接口 使用interface来定义一个接口.接口定义同类的定义类似,也是分为接口的声明和接口体,当中接口体由常量定义和方法定义两部分组成.定义接口的基本格式例如以下: [修饰符] inter ...