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 ...
随机推荐
- 通过gdb跟踪进程调度分析进程切换的过程
作者:吴乐 山东师范大学 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 本实验目的:通过gdb在lin ...
- 【随便走走】Vietnam
从来没有一个地方让我如此留念过. 初到越南印象就是乱,满街轰轰轰的摩托车,狭窄的街道,各种小酒店小商店.从机场出来的路上还看到了不少中国品牌如豪爵摩托等等. 落地办理了落地签,从大陆是不能办的.越 ...
- bzoj 1419 Red is good(期望DP)
[题意] R红B蓝,选红得1选蓝失1,问最优状态下的期望得分. [思路] 设f[i][j]为i个Rj个B时的最优期望得分,则有转移式为: f[i][j]=max{ 0,(f[i-1][j]+1)*(i ...
- 直线相交 POJ 1269
// 直线相交 POJ 1269 // #include <bits/stdc++.h> #include <iostream> #include <cstdio> ...
- web服务器分析与设计(四)
上篇已经开始了系统内部的分析,并且得到一些分析对象.在整个动作场景中,我们得到了一些粗略的对象.有必要对对象进行分析,合并,再抽象. 实质是职责的合理分配,使得系统合乎功能性,同时得到最大的可扩展,可 ...
- 用原生javascript模拟经典FC游戏公路争霸
#用原生javascript模拟经典FC游戏公路争霸 前几天看了园子里面的随笔 [原生javascript开发仿微信打飞机小游戏](http://www.cnblogs.com/Mr-Nobody/p ...
- String比较
String str1 = "abc"; String str2 = "abc"; String str3 = new String("abc&quo ...
- 给sublime text添加ubuntu launcher快捷方式
1.下载sublime text 2文件,解压并复制到/opt目录,文件夹名称不要含有空格 2.在/usr/share/applications目录下新建sublime_text.desktop文件 ...
- 树莓派使用8188eu无线网卡
#已经集成了8188eu驱动的镜像 http://cassidy.pi3g.com/rpi_images/raspbian-wifi-fix130523.7z #需要修改的信息 sudo nano / ...
- Signs of a poorly written jQuery plugin 翻译 (Jquery插件开发注意事项,Jquey官方推荐)
原文链接:http://remysharp.com/2010/06/03/signs-of-a-poorly-written-jquery-plugin/ 原文作者:remy sharp So far ...