今天明确了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的更多相关文章

  1. Codeforces 468B Two Sets 并查集

    题目大意:给出n个数,要求将n个数分配到两个集合中,集合0中的元素x,要求A-x也再0中,同理1集合. 写了几个版本号,一直WA在第8组数据...最后參考下ans,写了并查集过了 学到:1.注意离散的 ...

  2. codeforces 468B two set(并查集)

    链接 B. Two Sets 题意 给两个集合A B,两个数a b,n个数x,分配n个数到两个集合,要求x , a-x在同一个集合,x , b-x在同一个集合,属于A集合的数输出0,B的输出1,无解输 ...

  3. 算法笔记--2-sat

    强连通分量的应用,详见<挑战程序设计>P324 例题1:HDU Peaceful Commission 思路:强连通分量分解,看有没有两个同一个国家的代表在一个强连通分量里,如果有,就是N ...

  4. Codeforces 424A (思维题)

    Squats Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Statu ...

  5. Codeforces Round #499 (Div. 2) D. Rocket题解

    题目: http://codeforces.com/contest/1011/problem/D This is an interactive problem. Natasha is going to ...

  6. codeforces gym #101987K -TV ShowGame(2-SAT)

    题目链接: https://codeforces.com/gym/101987 题意: 有长度为$n$的只包含$B,R$的字符串 有m种关系,每个关系说出三个位置的确切字符 这三个位置的字符最多有一个 ...

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

  8. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  9. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

随机推荐

  1. 【转】移动web资源整理

    目录(更新于20150311) meta基础知识 H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 忽略将页面中的数字识别为电话号码 忽略Android平台中对邮箱地址的识别 当网站添加到主屏幕快速 ...

  2. Js原型模式

    function Person(){ } Person.prototype.name = "xd"; Person.prototype.age = 26; Person.proto ...

  3. HDU 1892-See you(二维BIT)

    题意: 最多1000*1000的方格,各方格开始有一本书 有四种操作:对指定方格把书拿走或向里面放书,从一个方格那一定量的书放到另一个方格,查询给定对角线顶点的坐标的矩形范围内包含的书的总数 分析: ...

  4. lightoj 1024 (高精度乘单精度)

    题意:给你一些数,求它们的最小公倍数,结果可能会很大. 统计出每个素因子出现的最大次数,把他们相乘即可,需要高精度. #include<cmath> #include<cstdio& ...

  5. 数字图像处理-----主成成分分析PCA

    主成分分析PCA 降维的必要性 1.多重共线性--预测变量之间相互关联.多重共线性会导致解空间的不稳定,从而可能导致结果的不连贯. 2.高维空间本身具有稀疏性.一维正态分布有68%的值落于正负标准差之 ...

  6. 使用matplotlib绘制带图例的图表

    #coding=utf8 from pylab import * plt.figure(figsize=(8,10), dpi=50) plt.plot(do_tow2[28:508],do_prn2 ...

  7. AHOI2013 Round2 Day1 简要题解

    第一题,好吧这是个dp.(搜素也能在BZOJ上卡过). 第二题,BFS搜索碰到的立方体面数,智硬没有想到... 第三题,其实一看就有思路,但关键是求x坐标不交的矩形对数+y坐标不交的矩形对数 - x, ...

  8. homework-03 图形化化最大子序列和

    你现在使用的代码规范是什么,  和上课前有什么改进? 我们一开始使用的是C++完成的相关程序.本次因为一些原因,改为C#进行编写.因为2013-10-21在VS2012中,所以所有的代码都已经被VS自 ...

  9. JSF 2 radio buttons example

    In JSF, "h:selectOneRadio" tag is used to render a set of HTML input element of type " ...

  10. VIM技巧(1)

    VIM技巧(1) 替换 36s/^\(.* = \)entity.\(.*\)$/\1this.GetShowName("\2",\2); 删除空行 %g/^$/d %g/^\s* ...