codeforces 468B 2-sat
今天明确了2-SAT;
表示对一对整数之间的关系是否存在
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<map>
using namespace std;
const int Maxn = 1e5+10;
int mark[Maxn << 1];
int s[Maxn * 2],top,tp;
int head[Maxn*2];
map <int,int> mp;
int x[Maxn * 2];
struct {
int v,next;
}E[Maxn << 2];
void init(){
memset(head,-1,sizeof(head));
memset(E,-1,sizeof(E));
tp = 0;
}
int n,m,a,b;
void addedge(int u,int v){
E[tp].v = v;
E[tp].next = head[u];
head[u] = tp++;
}
bool dfs(int x){
if(mark[x^1])return false;
if(mark[x])return true;
mark[x] = true;
s[top++] = x;
for(int i=head[x];i!=-1;i=E[i].next){
int v = E[i].v;
if(!dfs(v))return false;
}
return true;
}
bool solve(){
for(int i=0;i<n*2;i+=2){
if(!mark[i] && !mark[i+1]){
top = 0;
if(!dfs(i)){
while(top > 0)mark[s[--top]] = false;
if(!dfs(i+1)) return false;
}
}
}
return true;
}
int main(){ init();
cin >> n >> a >> b;
for(int i = 1;i<= n;i++){
scanf("%d",&x[i]);
mp[x[i]] = i;
}
for(int i=1;i<=n;i++){
int val = x[i],s = i;
s--;
int t = mp[a - val];t--;
if(!mp[a-val])addedge(2*s,2*s+1);
else {
addedge(2*s,2*t);
addedge(2*t+1,2*s+1);
}
t=mp[b-val]; t--;
if(!mp[b-val])
addedge(2*s+1,2*s);
else{
addedge(2*s+1,2*t+1);
addedge(2*t,2*s);
}
}
if(!solve()){
puts("NO");
}
else {
printf("YES\n");
for(int i=0;i<2*n;i+=2){
if(i!=0)
printf(" ");
if(mark[i])
printf("0");
else
printf("1");
}
printf("\n");
}
}
codeforces 468B 2-sat的更多相关文章
- Codeforces 468B Two Sets 并查集
题目大意:给出n个数,要求将n个数分配到两个集合中,集合0中的元素x,要求A-x也再0中,同理1集合. 写了几个版本号,一直WA在第8组数据...最后參考下ans,写了并查集过了 学到:1.注意离散的 ...
- codeforces 468B two set(并查集)
链接 B. Two Sets 题意 给两个集合A B,两个数a b,n个数x,分配n个数到两个集合,要求x , a-x在同一个集合,x , b-x在同一个集合,属于A集合的数输出0,B的输出1,无解输 ...
- 算法笔记--2-sat
强连通分量的应用,详见<挑战程序设计>P324 例题1:HDU Peaceful Commission 思路:强连通分量分解,看有没有两个同一个国家的代表在一个强连通分量里,如果有,就是N ...
- Codeforces 424A (思维题)
Squats Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Statu ...
- Codeforces Round #499 (Div. 2) D. Rocket题解
题目: http://codeforces.com/contest/1011/problem/D This is an interactive problem. Natasha is going to ...
- codeforces gym #101987K -TV ShowGame(2-SAT)
题目链接: https://codeforces.com/gym/101987 题意: 有长度为$n$的只包含$B,R$的字符串 有m种关系,每个关系说出三个位置的确切字符 这三个位置的字符最多有一个 ...
- Codeforces Round #620 (Div. 2) A. Two Rabbits
Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a p ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
随机推荐
- View.VISIBLE、INVISIBLE、GONE的区别
android中UI应用的开发中经常会使用view.setVisibility()来设置控件的可见性,其中该函数有3个可选值,他们有着不同的含义: View.VISIBLE--->可见View. ...
- 动态生成图片 保存到OutputStream
#region 把图片Copy到输出流 //获得图片全路径 string path = context.Server.MapPath("~/img/158_003.jpg"); / ...
- [Everyday Mathematics]20150208
对 $f\in C^2(\bbR)$ 适合 $$\bex \vlm{|x|}f(x)=0, \eex$$ 试证: $$\bex \int_{\bbR} |f'|^p\rd x \leq (p-1)^\ ...
- 常见设计模式的解析和实现(C++)之九—Decorator模式
作用:动态地给一个对象添加一些额外的职责.就增加功能来说,Decorator模式相比生成子类更为灵活. UML结构图: 抽象基类: 1) Component :定义一个对象接口,可以为这个接口动态地 ...
- jQuery-validate error messages in Twitter ...
http://www.jefclaes.be/2012/11/jquery-validate-error-messages-in.html something satisfactory. In thi ...
- append some buttons to the standard datagrid pager bar
<script type="text/javascript"> $(function(){ var pager = $('#dg').datagrid('getP ...
- Homework-10 BASIC
对于本次作业: 我的整体思路如下: 1.首先修改二维数组求最大子数组和的C语言代码,加入分步骤的当前最优解边界值,局部最优解的记录,使之支持分步执行,连续执行,回滚等功能. 2.将程序改写为Javas ...
- homework-06&homework-09
homework-06 1) 把程序编译通过, 跑起来 , 把正确的 playPrev(GoMove) 的方法给实现了. public void playPrev(GoMove gm) { // ne ...
- HDU 5676 ztr loves lucky numbers (模拟)
ztr loves lucky numbers 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/I Description ztr ...
- Spring EL ternary operator (if-then-else) example
Spring EL supports ternary operator , perform "if then else" conditional checking. For exa ...